The onchange event of select or text needs to be manually changed (through keyboard input) to trigger. If you assign a value to select or text in js, the onchang event cannot be triggered.
For example, after the page is loaded, an onChange event needs to be triggered. Use document.getElementById("province").value="Hubei"; it is not possible to directly assign a value to select or text. If you want to manually trigger the onchange event in js, it is not possible to directly assign a value to the select or text. , after js assigning the value to select, the following statement is added
document.getElementById("province").fireEvent('onchange') to implement,
The code copy is as follows:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Unt titled document</title>
<script type="text/javascript">
var provinces = new Array();
provinces["Hubei"] = ["Wuhan","Xiangyang","Suizhou","Yichang","Shiyan"];
provinces["Sichuan"] = ["Chengdu","Neijiang","Dazhou"];
provinces["Henan"] =["Zhengzhou","Nanyang","Xinyang","Luohe"];
function changeProvince()
{
var prov = document.getElementById("province").value;
var city =document.getElementById("city");
city.options.length =0;
for(var i in provinces[prov])
{
city.options.add(new Option(provinces[prov][i],provinces[prov][i]));
}
}
window.onload = function(){
var province = document.getElementById("province");
for(var index in provinces)
{
//alert(index);
province.options.add(new Option(index,index));
}
province.fireEvent("onchange");
};
</script>
</head>
<body>
Province:<select id="province" onchange= "changeProvince()"></select>
City:<select id="city"></select>
</body>
</html>