当前位置: 首页 > article >正文

layui 实现 城市联动

<div class="layuimini-container">
    <form id="app-form" class="layui-form layuimini-form">
        
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" class="layui-input"  placeholder="请输入标题" value="{$row.title|default=''}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">缩略图</label>
            <div class="layui-input-block layuimini-upload">
                <input name="image" class="layui-input layui-col-xs6"   placeholder="请上传缩略图" value="{$row.image|default=''}">
                <div class="layuimini-upload-btn">
                    <span><a class="layui-btn" data-upload="image" data-upload-number="one" data-upload-exts="png|jpg|ico|jpeg" data-upload-icon="image"><i class="fa fa-upload"></i> 上传</a></span>
                    <span><a class="layui-btn layui-btn-normal" id="select_image" data-upload-select="image" data-upload-number="one" data-upload-mimetype="image/*"><i class="fa fa-list"></i> 选择</a></span>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input type="text" name="description" class="layui-input"  placeholder="请输入描述" value="{$row.description|default=''}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择省市</label>
            <div class="layui-input-inline">
              <select name="province" lay-filter="magazine">
                <option value="">请选择省</option>
                {foreach $provinceList as $key=>$val}
                <option value="{$val.id}" {if $val.name == $row.province}  selected {/if}>{$val.name}</option>
                {/foreach}
              </select>
            </div>
            <div class="layui-input-inline">
              <select name="city">
                <option value="{$row.city}">{$row.city}</option>
              </select>
            </div>
          </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地区</label>
            <div class="layui-input-block">
                <input type="text" name="address" class="layui-input"  placeholder="请输入地区" value="{$row.address|default=''}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input type="text" name="phone" class="layui-input"  placeholder="请输入电话" value="{$row.phone|default=''}">
            </div>
        </div>

        <div class="hr-line"></div>
        <div class="layui-form-item text-center">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit>确认</button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
        </div>

    </form>
</div>

<script>
    layui.use('form', function(){
        var form = layui.form;
        form.on('select(magazine)', function(data){
            var areaId=data.elem.value;
            $.ajax({
                type: 'POST',
                url: "/api/member/getCity",
                data: {parent_id:areaId},
                dataType:  'json',
                success:function(e){
                    console.log(e.data);
                    //empty() 方法从被选元素移除所有内容
                    $("select[name='city']").empty();
                    var html = "<option value=''>请选择市<option>";
                    $(e.data).each(function (v, k) {
                        html += "<option value='" + k.city_id + "'>" + k.name + "</option>";
                    });
                    //把遍历的数据放到select表里面
                    $("select[name='city']").append(html);
                    //从新刷新了一下下拉框
                    form.render('select');      //重新渲染
                }
            });
        });
 
    });
</script>

PHP 代码

        /**
     * @NodeAnotation(title="获取城市")
     */
    public function getCity()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['parent_id']) && $param['parent_id'] != '') {
            $where['upid'] = $param['parent_id'];
        } else {
            $where['level'] = 0;
        }
        $city = Db::name('city')->field('id,level,name,upid')->where($where)->select();
        return Json(['code' => 200, 'msg' => '获取成功', 'data' => $city]);
    }


http://www.kler.cn/a/376626.html

相关文章:

  • 全新更新!Fastreport.NET 2025.1版本发布,提升报告开发体验
  • 终于把DETR搞懂了!Detection Transformer架构详解及使用方法说明
  • 企业如何通过架构蓝图实现数字化转型
  • 如何在生产环境中实施定期备份策略?
  • WonderWorld: Interactive 3D Scene Generation from a Single Image 论文解读
  • Hadoop期末复习(完整版)
  • git cherry-pick用法详解
  • 顺序表和链表(一)
  • jmeter结合ansible分布式压测--准备工作
  • 深入了解嵌入式硬件设计
  • 视频智能分析平台LiteAIServer入侵检测算法平台部署行人入侵检测算法:智能安防的新利器
  • 钉钉内集成第三方免密登录(Vue+.Net)
  • 定制化视频生成新模范!零样本主体驱动,精确运动控制!复旦阿里等发布DreamVideo-2
  • 算法笔记day10
  • CentOS下Redis简洁安装(无坑版)
  • LocalDate 类常用方法详解(日期时间类)
  • 图文深入介绍Oracle DB link(三)
  • Python世界:自动化办公Word之批量替换文本生成副本
  • C++ 实现俄罗斯方块游戏
  • 贪心算法习题其二【力扣】【算法学习day.19】
  • Selenium自动化测试框架(附教程+文档)
  • Rust 力扣 - 2134. 最少交换次数来组合所有的 1 II
  • 游戏光照的专业知识解析
  • 网络学习/复习3序列化与反序列化/HTTP/HTTPS
  • 了解SQLExpress数据库
  • 文件系统(IO-进程-线程)