省市縣聯動列表資料整理

2021-10-04 02:34:25 字數 1243 閱讀 7065

[,,.

...,

,]

$(function () 

if(val.leveltype==2)

if(val.leveltype==3)

})for(let i=province.length-1;i>=0;i--);//,children:}

list.p=province[i];

let cityarr = ; //市資料

for(let c=city.length-1;c>=0;c--);

if(city[c].parentid == province[i].id)

}children.a=areaarr;

city.splice(c,1);

cityarr.push(children)

}list.children=cityarr;

}// list.c=cityarr;

province.splice(i,1);

obj.push(list)

}console.log(obj)}})

})

最後資料變成:

[,

"children": [,

"a":

}, ,

"a":

} }]

}]

其中有乙個大坑:

for in遍歷陣列的時候進行area.splice(a,1);函式計算的時候會報錯。 因為splice刪除陣列以後會進行重新下標計算, 所以報錯undefined,用倒敘的方法可以避免這個問題。

資料處理完以後就可以進行聯動篩查了:

function addressselection(provincelist)

addressselection.prototype.findprovince = function())

return province;

}addressselection.prototype.findcity = function(index))

return city;

}addressselection.prototype.findarea = function(ind,pind))

return area;

}var demo = new addressselection(provincelist);

有其他更好的辦法 歡迎交流~

ajax省市縣聯動

html form id form1 runat server div divid dv table tr td asp dropdownlist id ddlprovince runat server onchange provincechange asp dropdownlist td td s...

js省市縣聯動

select 或text的onchange事件需要手動 通過鍵盤輸入 改變select或text的值才能觸發,如果在js中給select或text賦值,則無法觸發onchang事件,例如,在頁面載入完成以後,需要觸發乙個onchange事件,在js中用document.getelementbyid ...

全國省市縣資料爬蟲

專案需要全國省市縣資料,網上找了一圈發現要麼過時要麼收費,於是花點時間自己寫了個爬蟲爬了些基礎資料,基本上夠用了,資料是從國家統計局爬來的,目前更新到2019年,如下 s.mount http httpadapter max retries 2 重試次數 r s.get url,headers he...