jsトリガselect onchangeイベントコード


selectまたはtextのonchangeイベントは手動(キーボードで入力)でselectまたはtextの値を変更してトリガする必要があります。jsでselectまたはtextに値を付与すると、onchangイベントをトリガできません。selectやtextに直接値を付けてはいけません。手動トリガonchangeイベントを実現するには、jsがselectに値を付けた後、次の文document.getElementById(「province」)・fireEvent(「onchange」)を入れて実現します。
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> </title>
<script type="text/javascript">
var provinces = new Array();
provinces[" "] = [" "," "," "," "," "];
provinces[" "] = [" "," "," "];
provinces[" "] =[" "," "," "," "];
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>
:<select id="province" onchange= "changeProvince()"></select>
:<select id="city"></select>
</body>
</html>