Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to implement drop-down box linkage with JS
This time I will bring you a detailed explanation of the steps to implement drop-down box linkage with JS. What are the precautions for JS to implement drop-down box linkage? The following is a practical case, let's take a look.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> JS下拉联动</title> <script> function setSecond(obj){ var val = obj.value; if(val == 'en'){ var sec = document.getElementById('second'); sec.innerHTML = "<option>one</option><option>two</option>"; }else{ var sec = document.getElementById('second'); sec.innerHTML = "<option>一</option><option>二</option>"; } } </script> </head> <body> <p> <select id="first" onchange="setSecond(this)"> <option value="en">en</option> <option value="zh">zh</option> </select> </p> <p> <select id="second"> </select> </p> </body> </html>Use innerHTML,
IE browser does not support this method, so the improvement method is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS下拉联动</title> <script> function setSecond(obj){ var val = obj.value; if(val == 'en'){ var sec = document.getElementById('second'); sec.options[0] = new Option("one","one"); sec.options[1] = new Option("two","two"); }else{ var sec = document.getElementById('second'); sec.options[0] = new Option("一","one"); sec.options[1] = new Option("二","two");//可设置循环配置,也可一个一个配置 } } </script> </head> <body> <p> <select id="first" onchange="setSecond(this)"> <option value="en">en</option> <option value="zh">zh</option> </select> </p> <p> <select id="second"> </select> </p> </body> </html>Can be compatible with Firefox, IE, etc. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
NodeJs mobile phone access local server case analysis
$emit and $on father-son brother components in vue Detailed operation explanation
The above is the detailed content of Detailed explanation of the steps to implement drop-down box linkage with JS. For more information, please follow other related articles on the PHP Chinese website!