<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>選擇</title>
<meta name="viewport" content="initial-scale=1.0, width=device-width,minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
</head>
<body>
<select name="channel" id="channel" onChange="getVal(this.options[this.selectedIndex].value)">
<option value="請選擇">-請選擇-</option>
<option value="即使互動">即使互動</option>
<option value="光影無限">光影無限</option>
<option value="樂客渠道">樂客渠道</option>
</select>
<select name="retailer" id="retailer">
<option>-請選擇-</option>
</select>
</body>
<script>
//onChange="getVal(this.options[this.selectedIndex].value)"
//利用onChange事件,來擷取當前選項的value
//通過this.options拿到當前select的所有option選項
//通過this.selectedIndex拿到被選選項的索引號
//這樣就能拿到每次選項的value值,然後再用資料進行匹配,拿到該選項的相應的資料,並填充到下一個select
//定義資料Array=[["value","資料1|資料2|資料3"],["value","資料1|資料2|資料3"]]。
//定義資料裡的value與你option裡的value一致
var valArray = [["即使互動","即使互動1|即使互動2|即使互動3|即使互動4"],["光影無限","光影無限1|光影無限2|光影無限3|光影無限4"],["樂客渠道","樂客渠道1|樂客渠道2|樂客渠道3|樂客渠道4"],["請選擇","-請選擇-"]]
function getVal(currVal)
{
//當前所選擇的項 ,通過參數 currVal 獲得
var currVal = currVal;
//提前聲明變數,記住javascript只有函數範圍,沒有塊級範圍,提前聲明是一個好習慣
var i,j;
//清空子下拉選單 document.all.id 選中子id
//如果你不知道document.all,可以尋找相關資料哦
document.all.retailer.length = 0 ;
for (i = 0 ;i <valArray.length;i++)
{
//得到 當前項 在 數組中的位置
if(valArray[i][0]==currVal)
{
//得到 當前項 所有的子資料
var tmpValArray = valArray[i][1].split("|")
for(j=0;j<tmpValArray.length;j++)
{
//填充 下拉選單
document.all.retailer.options[document.all.retailer.length] = new Option(tmpValArray[j],tmpValArray[j]);
}
}
}
}
//看完之後,要不要自己動手練一下呢?
//有沒有發現也很簡單呢,也許你也可以考慮一下三級聯動
//整體思路就是,擷取你當前選項,準備好有規律的資料格式,用當前選項的值去你的資料裡匹配,然後把匹配的值填充到下一個select裡面,填充之前記得清空一下哦。
</script>
</html>