需要預設選取時,定義一個變數var cityId=城市id
下面是js程式碼
function readxml() {
var XmlDoc = null;
if (window.ActiveXObject) {
XmlDoc = new ActiveXObject("Microsoft.XMLeXObject) {
XmlDoc = new ActiveXObject("Microsoft.XMLMM"); .async = false;
XmlDoc.load(path "/web/common/regions.xml");
browse = "ie";
} else if (document.implementation
&& document. implementation.createDocument) {
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load("regions.xml");
browse = "ff";
} else {
alert('未做與該瀏覽器的兼容!');
}
var root = XmlDoc.documentElement;
// 取得根節點下面的省節點
var provinces = root.childNodes;
var province = document.getElementByIdx_x_x("province");
var cities = document.getElementByIdx_x_x("regId");
for ( var i = 0; provinces.length; i ) {
// 取得省節點的name屬性的值
var name = provinces[i].getAttribute("name");
// 建立一個option
var opt = document_createElement_x_x("option");
// 為option新增文字
opt.a(document_createTextNode(name));
// 加入父節點中
if(cityId! !=""){
var citys=provinces[i].childNodes;
for(var j=0;jif(citys[j].getAttribute( "id")==cityId){
opt.selected="selected";
for ( var j = 0; j // 建立一個option
var opt1 = document_createElement_x_x("option");
opt1.value = citys[j].getAttribute("id");
// 為option新增文字
opt1.a(document_createa(document_createa(document) ]
.getAttribute("name")));
// 加入到父節點中
if(citys[j].getAttribute("id")==cityId){
opt1. selected="selected";
}
cities.a(opt1);
}
}
}
}
province.a(opt);
province.onchange = function() {
var pce = document.getElementByIdx_x_x("province");
var pce = pce.options;
var opt1 = opts[pce.selectedIndex]; 🎜>var name = opt1.innerText;
for ( var i = 0; i // 取得省節點的name屬性的值
var name1 = provinces[i ].getAttribute("name");
if (name == name1) {
cities.length = 1;// 每次改變的時候清空
var pros = provinces[i];
var citys = pros.childNodes;
for ( var j = 0; j // 建立一個option
var opt1 = document_createElement_x_x("option"); >opt1.value = citys[j].getAttribute("id");
// 為option新增文字
opt1.a(document_createTextNode(citys[j]
.getAttribute("name")) );
// 添加到父節點中
cities.a(opt1);
}
}
}
}
}
以下是xml結構