select or text onchange events require manual (through keyboard input) to change the value of select or text to trigger, this article describes the use of JS to trigger select OnChange events
The onchange event for select or text requires that the value of the Select or text be changed manually (through keyboard input) to trigger, and if you assign a value to select or text in JS, you cannot trigger the Onchang event, for example, after the page load is complete, Need to trigger a onchange event, use document.getElementById ("province") in JS. value= "Hubei"; it is not possible to assign values directly to select or text, and to implement manual triggering of onchange events, After JS assigns a value to select, add the following statement document.getElementById ("province"). FireEvent (' onchange ') to implement the, code as follows: <head> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title> Untitled document </title> <script type=" Text/javascript "> var Provinces = new Array (); provinces["Hubei"] = ["Wuhan", "Xiangyang", "Suizhou", "Yichang", "Shiyan"]; provinces["Sichuan"] = ["Chengdu", "Neijiang", "Dazhou"];& nbsp 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 i N provinces) { //alert (index); province.options.add (New Option (Index,index)); } Province.fireevent ("onchange"); }; </script> </head> <body> province:< Select Id= "Province" onchange= "Changeprovince ()" ></select> cities: <select id= "City" ></select > </body> </html>