<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script>
<body>
<div>
<ul id="package">
</ul>
</div>
<div>
<ul id="price">
</ul>
</div>
<div>
<ul id="details">
</ul>
</div>
<script>
var city = [
{
"id": 10002,
"name": "陕西省",
"child": [
{
"id": 100011,
"name": "西安市",
"child_name": "高新区",
},
{
"id": 100012,
"name": "宝鸡市",
"child_name": "新区",
}
]
},
{
"id": 10001,
"name": "湖北省",
"child": [
{
"id": 100011,
"name": "咸宁市",
"child_name": "崇阳",
}
],
},
{
"id": 10002,
"name": "陕西省",
"child": [
{
"id": 100011,
"name": "西安市",
"child_name": "高新区",
},
{
"id": 100012,
"name": "宝鸡市",
"child_name": "新区",
}
]
},
{
"id": 10003,
"name": "甘肃省",
"child": [
{
"id": 100021,
"name": "兰州市",
"child_name": "兰州区",
},
{
"id": 100022,
"name": "酒泉市",
"child_name": "酒泉区",
}
]
}
]
$(document).ready(function () {
// $("#price-radio").hide();
for (var i = 0; i < city.length; i++) {
var package_r = "<input id = 'do_package' type='radio' name='id' value=" + city[i].id + " package_id=" + i + ">"
var content = $("<span></span>").text(city[i].name);
$("#package").append(package_r, content);
$("#do_package").prop("checked", true);
}
});
$(document).ready(function () {
var child = city[0]['child'];
if (child.length < 2) {
//添加span
var content_p = $("<span></span>").text(child[0].name);
var content_p_s = $("<span></span>").text(child[0].child_name);
$("#price").append(content_p);
$("#details").append(content_p_s);
} else {
//添加radio
for (var j = 0; j < child.length; j++) {
var price_r = "<input id = 'do_price' type='radio' name='id_p' value=" + child[j].id + " price_id=" + j + " package_id=" + 0 + ">"
var content_p = $("<span></span>").text(child[j].name);
$("#price").append(price_r, content_p);
$("#do_price").prop("checked", true);
}
var content_p_s = $("<span></span>").text(child[0].child_name);
$("#details").append(content_p_s);
}
});
//单选转换(一级)
$('body').on('change', '#do_package', function () {
//清除元素
$("#price").empty();
$("#details").empty();
var package_id = $(this).attr("package_id");
//判断数组长度
var content = city[package_id]['child'];
var length = city[package_id]['child'].length;
if (length < 2) {
//添加span
var content_p = $("<span></span>").text(content[0].name);
var content_p_s = $("<span></span>").text(content[0].child_name);
$("#price").append(content_p);
$("#details").append(content_p_s);
} else {
//添加radio
for (var k = 0; k < content.length; k++) {
var price_r = "<input id = 'do_price' type='radio' name='id_p' value=" + content[k].id + " price_id=" + k + " package_id=" + package_id + ">"
var content_p = $("<span></span>").text(content[k].name);
$("#price").append(price_r, content_p);
$("#do_price").prop("checked", true);
}
var content_p_s = $("<span></span>").text(content[0].child_name);
$("#details").append(content_p_s);
}
});
//二级切换
$('body').on('change', '#do_price', function () {
//清除元素
$("#details").empty();
var price_id = $(this).attr("price_id");
var package_id = $(this).attr("package_id");
// //填充详情
var content = city[package_id]["child"][price_id];
var content_p_s = $("<span></span>").text(content.child_name);
$("#details").append(content_p_s);
});
</script>
</body>
</html>
二级列表
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 之前在网上看到过这种的,都是封装的或者是用的第三方,这是自己写的,有兴趣大家也可以把他封装起来留着自己用; #im...