layui省市区联动选择的实现
⾸先layui需要引⽤这两个⽂件(下⾯是我的路径)
<script src="../../common/util/layui/layui.js"></script>
<link rel="stylesheet" href="../../common/util/layui/css/layui.css">
省市区数据信息
var threeSelectData={"北京":{val:"110000",items:{"北京":{val:"110100",items:{"东城区":"110101","西城区":"110102","崇⽂区":"110103","宣武区":"110104","朝阳区":" JS
var defaults = {
s1: 'provid',
s2: 'cityid',
s3: 'areaid',
v1: null,
v2: null,
v3: null
};
var $form;
var form;
var $;
layui.define(['jquery', 'form'], function () {
$ = layui.jquery;
form = layui.form;
$form = $('form');
treeSelect(defaults);
});
function treeSelect(config) {
config.v1 = config.v1 ? config.v1 : 110000;
config.v2 = config.v2 ? config.v2 : 110100;
config.v3 = config.v3 ? config.v3 : 110101;
$.each(threeSelectData, function (k, v) {
appendOptionTo($form.find('select[name=' + config.s1 + ']'), k, v.val, config.v1);
});
cityEvent(config);
areaEvent(config);
<('select(' + config.s1 + ')', function (data) {
cityEvent(data);
<('select(' + config.s2 + ')', function (data) {
areaEvent(data);
});
});
function cityEvent(data) {
$form.find('select[name=' + config.s2 + ']').html("");
config.v1 = data.value ? data.value : config.v1;
$.each(threeSelectData, function (k, v) {
if (v.val == config.v1) {
if (v.items) {
$.each(v.items, function (kt, vt) {
appendOptionTo($form.find('select[name=' + config.s2 + ']'), kt, vt.val, config.v2);
});
}
}
});
config.v2 = $('select[name=' + config.s2 + ']').val();
config.v2 = $('select[name=' + config.s2 + ']').val();
areaEvent(config);
}
function areaEvent(data) {
$form.find('select[name=' + config.s3 + ']').html("");
config.v2 = data.value ? data.value : config.v2;
$.each(threeSelectData, function (k, v) {
if (v.val == config.v1) {
if (v.items) {
$.each(v.items, function (kt, vt) {
if (vt.val == config.v2) {
$.each(vt.items, function (ka, va) {
appendOptionTo($form.find('select[name=' + config.s3 + ']'), ka, va, config.v3); });
}
});
}
}
});
<('select(' + config.s3 + ')', function (data) { });
}
function appendOptionTo($o, k, v, d) {
var $opt = $("<option>").text(k).val(v);
if (v == d) { $opt.attr("selected", "selected") }
$opt.appendTo($o);
}
}
前端界⾯
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">选择地区</label>
<div class="layui-input-inline">
<select name="provid" id="provid" lay-filter="provid">
<option value="">请选择省</option>
</select>
</div>
<div class="layui-input-inline">
<select name="cityid" id="cityid" lay-filter="cityid">
<option value="">请选择市</option>
</select>
</div>
<div class="layui-input-inline">
<select name="areaid" id="areaid" lay-filter="areaid">
<option value="">请选择县/区</option>
</select>
</div>
</div>
</form>
源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>layui省市区联动选择的实现</title>
<script type="text/javascript" src="code.jquery/jquery.js"></script>
<script src="../../common/util/layui/layui.js"></script>
<link rel="stylesheet" href="../../common/util/layui/css/layui.css">
<link rel="stylesheet" href="../../common/util/layui/css/layui.css">
<script type="text/javascript">
var threeSelectData={"北京":{val:"110000",items:{"北京":{val:"110100",items:{"东城区":"110101","西城区":"110102","崇⽂区":"110103","宣武区":"110104","朝阳 var defaults = {
s1: 'provid',
s2: 'cityid',
s3: 'areaid',
v1: null,
v2: null,
v3: null
};
var $form;
var form;
var $;
layui.define(['jquery', 'form'], function () {
$ = layui.jquery;
form = layui.form;
$form = $('form');
treeSelect(defaults);
});
function treeSelect(config) {
config.v1 = config.v1 ? config.v1 : 110000;
config.v2 = config.v2 ? config.v2 : 110100;
config.v3 = config.v3 ? config.v3 : 110101;
$.each(threeSelectData, function (k, v) {
appendOptionTo($form.find('select[name=' + config.s1 + ']'), k, v.val, config.v1);
});
cityEvent(config);
areaEvent(config);
<('select(' + config.s1 + ')', function (data) {
cityEvent(data);
<('select(' + config.s2 + ')', function (data) {
areaEvent(data);
});
});
function cityEvent(data) {
$form.find('select[name=' + config.s2 + ']').html("");
config.v1 = data.value ? data.value : config.v1;
$.each(threeSelectData, function (k, v) {
if (v.val == config.v1) {
if (v.items) {
$.each(v.items, function (kt, vt) {
appendOptionTo($form.find('select[name=' + config.s2 + ']'), kt, vt.val, config.v2);
});
}
}
});
config.v2 = $('select[name=' + config.s2 + ']').val();
areaEvent(config);
}
function areaEvent(data) {
$form.find('select[name=' + config.s3 + ']').html("");
config.v2 = data.value ? data.value : config.v2;
$.each(threeSelectData, function (k, v) {
if (v.val == config.v1) {
if (v.items) {
$.each(v.items, function (kt, vt) {
if (vt.val == config.v2) {
$.each(vt.items, function (ka, va) {
appendOptionTo($form.find('select[name=' + config.s3 + ']'), ka, va, config.v3);
});
}
});
}
}
});
<('select(' + config.s3 + ')', function (data) { }); }
function appendOptionTo($o, k, v, d) {
var $opt = $("<option>").text(k).val(v);
if (v == d) { $opt.attr("selected", "selected") }
$opt.appendTo($o);
}
}
</script>
</head>
<body>
<div >
<form class="layui-form">
<div class="layui-form-item">
<label class="layui-form-label">选择地区</label>
<div class="layui-input-inline">
<select name="provid" id="provid" lay-filter="provid"> <option value="">请选择省</option>
</select>
</div>
<div class="layui-input-inline">
<select name="cityid" id="cityid" lay-filter="cityid">
<option value="">请选择市</option>
</select>
</div>
<div class="layui-input-inline">
<select name="areaid" id="areaid" lay-filter="areaid"> <option value="">请选择县/区</option>
</select>
</div>
</div>
</form>
</div>
layui下载</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论