JavaScript學習第二堂課--①輸入判斷②省市聯動

來源:互聯網
上載者:User

標籤:方法   深圳   資料   ima   img   聯動   lang   country   .text   

try 可以設定提醒,用catch來捕獲傳回值

 

②省市聯動

迴圈填充的時候要先清空之前的,

 

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>

<script type="text/javascript">
var arr = ["中國","美國","日本"];

arr["中國"] = ["北京","上海","廣州","深圳"];
arr["美國"] = ["紐約","華盛頓","洛杉磯","芝加哥"];
arr["日本"] = ["東京","大阪","北海道"];

arr["北京"] = ["北京1","北京2","北京3","北京4"];
arr["上海"] = ["上海1","上海2","上海3","上海4"];
arr["廣州"] = ["廣州1","廣州2","廣州3","廣州4"];
arr["深圳"] = ["深圳1","深圳2","深圳3","深圳4"];

arr["紐約"] = ["紐約1","紐約2","紐約3","紐約4"];
arr["華盛頓"] = ["華盛頓1","華盛頓2","華盛頓3","華盛頓4"];
arr["洛杉磯"] = ["洛杉磯1","洛杉磯2","洛杉磯3","洛杉磯4"];
arr["芝加哥"] = ["芝加哥1","芝加哥2","芝加哥3","芝加哥4"];

arr["東京"] = ["東京1","東京2","東京3","東京4"];
arr["大阪"] = ["大阪1","大阪2","大阪3","大阪4"];
arr["北海道"] = ["北海道1","北海道2","北海道3","北海道4"];

function addData(arrData,itemID){
document.getElementById(itemID).length = 0;//先清空傳過來id的所有內容,再添加

for(var i = 0; i<arrData.length; i++){
var option = new Option();//O大寫,
option.text = arrData[i];//返回前台介面值
option.value = arrData[i];//返回資料庫值
document.getElementById(itemID).options.add(option);//options固定賦值方法
}
}
function fun(){
addData(arr,"country");
addData(arr[arr[0]],"province");
addData(arr[arr[arr[0]][0]],"area");
}

function changePro(country){
addData(arr[country],"province");
addData(arr[arr[country][0]],"area");//觸發地區,也顯示城市對應的地區
}
function changeArea(province){
addData(arr[province],"area");
}
</script>
</head>
<body onload="fun()">
國家:<select id="country" onchange="changePro(this.value)"></select>
城市:<select id="province" onchange="changeArea(this.value)" ></select>
地區:<select id="area"></select>
</body>
</html>

 

JavaScript學習第二堂課--①輸入判斷②省市聯動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.