2個下拉框,選擇1級菜單后,2級菜單出現相應的備選項。如果沒有選擇則不能提交。
先創建html文件
<!DOCTYPE html><html><head> <title></title></head><body> <form> <select id="province"> <option selected="selected">請選擇...</option> </select> <select id="city"> <option selected="selected">請選擇...</option> </select> <button type="submit" id="where_submit" disabled="disabled">提交</button> </form></body><script type="text/javascript">var provinces=['遼寧','北京','上海','吉林','浙江'];//最新添加的省份放在最前面var choice=['請選擇...']var zhejiang=['杭州','嘉興','寧波','紹興'];var shanghai=['金山','閘北','普陀','徐匯'];var jilin=['長春','遼源','吉林','四平'];var beijing=['海淀','朝陽','東城','西城'];var liaoning=['沈陽','大連','盤錦','錦州','遼陽','鞍山']//城市排序由后到前var citys=new Array;citys[0]=choice;citys[1]=zhejiang;citys[2]=jilin;citys[3]=shanghai;citys[4]=beijing;citys[5]=liaoning;function add_option(select,option){ var target=document.getElementById(select); for (var i = option.length - 1; i >= 0; i--) { var add_option=document.createElement("option"); add_option.text=option[i]; target.add(add_option,null); target.lastChild.setAttribute("name",option[i]); }}add_option("province",provinces);document.getElementById("province").addEventListener("change",function(){ var selevted_province=document.getElementById("province"); var selected_city=document.getElementById("city"); for (var i = selevted_province.length - 1; i >= 0; i--) { selected_city.remove(i); } var selected=selevted_province.selectedIndex; if (selected==0) { add_option("city",citys[0]); document.getElementById("where_submit").setAttribute("disabled","ture"); }else{ add_option("city",citys[selected]); document.getElementById("where_submit").removeAttribute("disabled"); }})</script></html>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答