大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
var data = [{
创新互联成立于2013年,是专业互联网技术服务公司,拥有项目成都做网站、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元浑源做网站,已为上家服务,为浑源各地企业和个人服务,联系电话:13518219792
id: 1,
pid: 0,
name: '水果'
},
{
id: 2,
pid: 0,
name: '蔬菜'
},
{
id: 3,
pid: 1,
name: '苹果'
},
{
id: 4,
pid: 1,
name: '香蕉'
},
{
id: 5,
pid: 2,
name: '萝卜'
},
{
id: 6,
pid: 2,
name: '茄子'
},
{
id: 7,
pid: 1,
name: '芒果'
},
{
id: 8,
pid: 1,
name: '桔子'
},
{
id: 9,
pid: 2,
name: '青瓜'
},
{
id: 10,
pid: 2,
name: '苦瓜'
}];
window.onload = function() {
var type_sel = document.createElement('select');
if (document.attachEvent) type_sel.attachEvent('onchange', change);
else type_sel.addEventListener('change', change, true);
createOptions(type_sel, 0);
}
function change(e) {
var o = e.srcElement || e.target,
child_sel = document.createElement('select'),
pid = o.value,
obj = document.getElementsByTagName('select')[1];
obj obj.parentNode.removeChild(obj);
createOptions(child_sel, pid);
}
function createOptions(sel, pid) {
var option = document.createElement('option');
option.value = '';
option.innerHTML = '请选择';
sel.appendChild(option);
for (var i in data) {
var d = data[i];
if (d.pid == pid) {
var option = document.createElement('option');
option.value = d.id;
option.innerHTML = d.name;
sel.appendChild(option);
}
}
document.body.appendChild(sel);
}
你好!
下面是个简单的小例子,仅供参考:
script type="text/javascript"
$(document).ready(function(){
var data={
"liaoning":{
"210100000000":"沈阳市",
"210200000000":"大连市",
"210300000000":"鞍山市"
},
"heilongjiang":{
"230100000000":"哈尔滨市",
"230200000000":"齐齐哈尔市",
"230300000000":"鸡西市"
}
};
$("#aa").change(function(){
if(this.value==0){
$("#bb").empty();
} else {
for(var key in data){
if(key==this.value){
$("#bb").empty();
for(var kkey in data[this.value]){
$("option /").attr("value",kkey).text(data[this.value][kkey]).appendTo($("#bb"));
}
}
}
}
});
});
/script
/head
body
select id="aa"
option value="0"请选择省/option
option value="liaoning"辽宁/option
option value="heilongjiang"黑龙江/option
/select
select id="bb"
/select
/body
希望对你有帮助!
1.首先将Id为province的添加class=“province”便于针对city隐藏
2.$("#province").on("change", function(){
var index = document.getElementById("province").selectedIndex;
$(".city:not(.city2)").hide();
$(".city2 option").eq(index).attr("selected",true);
});
selectedIndex即可获取选中的行数,根据行数联动city2即可。