欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js觸發(fā)select onchange事件的小技巧

 更新時間:2014年08月05日 10:27:25   投稿:whsnow  
select 或text的onchange事件需要手動改變select或text的值才能觸發(fā),下面有個不錯的方法可以通過js觸發(fā)select onchange事件

select 或text的onchange事件需要手動(通過鍵盤輸入)改變select或text的值才能觸發(fā),如果在js中給select或text賦值,則無法觸發(fā)onchang事件,
例如,在頁面加載完成以后,需要觸發(fā)一個onChange事件,在js中用document.getElementById("province").value="湖北";直接給select或text賦值是不行的,要想實現(xiàn)手動觸發(fā)onchange事件,需要在js給select賦值后,加入下面的語句

document.getElementById("province").fireEvent('onchange') 來實現(xiàn),

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<script type="text/javascript">

var provinces = new Array();
provinces["湖北"] = ["武漢","襄陽","隨州","宜昌","十堰"];
provinces["四川"] = ["成都","內(nèi)江","達州"];
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>

相關(guān)文章

最新評論