var?data?=?[{
創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元渾源做網(wǎng)站,已為上家服務(wù),為渾源各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話: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?=?'請(qǐng)選擇';
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);
}
你好!
下面是個(gè)簡(jiǎn)單的小例子,僅供參考:
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"請(qǐng)選擇省/option
option?value="liaoning"遼寧/option
option?value="heilongjiang"黑龍江/option
/select
select?id="bb"
/select
/body
希望對(duì)你有幫助!
1.首先將Id為province的添加class=“province”便于針對(duì)city隱藏
2.$("#province").on("change", function(){
var index = document.getElementById("province").selectedIndex;
$(".city:not(.city2)").hide();
$(".city2 option").eq(index).attr("selected",true);
});
selectedIndex即可獲取選中的行數(shù),根據(jù)行數(shù)聯(lián)動(dòng)city2即可。