JS triggers select onchange event code

Source: Internet
Author: User

 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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.