JS二級選單聯動遇到的錯誤
阿新 • • 發佈:2019-01-24
<!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>二級選單聯動</title> <style type="text/css"> select{ width:120px; border:2px solid #096; } </style> <script language="javascript"> function changeCity() { var arr=new Array(); var newOption; arr['中國']=['上海','廣州','武漢']; arr['美國']=['紐約','洛杉磯','華盛頓']; arr['日本']=['橫濱','東京','福岡']; var selectCountry=document.getElementById("country").value; var cityNode=document.getElementById("city"); cityNode.length=0; for(var city in arr[selectCountry]){ newOption = new Option(arr[selectCity][city]); cityNode.options.add(newOption); } } </script> <select id="country" onchange="changeCity()"> <option value="請選擇國家">請選擇國家</option> <option value="中國" >中國</option> <option value="美國">美國</option> <option value="日本">日本</option> </select> <select id="city"> </select> </body> </html>
不知道為什麼,一直將newOption加到第二個選單