沒有試圖在這裏跨瀏覽器...但是,只有一個瀏覽器,我可以認爲這可能給你帶來麻煩:)
Demo
代碼
var product = document.getElementById('product');
var power = document.getElementById('power');
var allOpts = power.getElementsByTagName('option');
var opts = {
empty: allOpts[0]
};
// This builds three additional lists of options for each type of product
// It relies on having a single attribute - if more are added, this logic
// would need to be adjusted.
for(var i = 1; i < allOpts.length; ++i) {
var name = allOpts[i].attributes[0].value;
opts[name] = opts[name] || [];
opts[name].push(allOpts[i]);
}
// Each time product is changed, just loop through based on the selected ID
// and add those options to the power select list (removing all children first)
product.addEventListener('change', function(evt) {
var val = evt.target.value;
power.innerHTML = '';
power.appendChild(opts.empty);
for(var i = 0; i < opts[val].length; ++i) {
power.appendChild(opts[val][i]);
}
});
這裏是使用documentFragment的版本:
Demo
DocumentFragment的版本
var product = document.getElementById('product');
var power = document.getElementById('power');
var allOpts = power.getElementsByTagName('option');
var opts = {
empty: allOpts[0]
};
// Appending to the doc fragment removes it from the live nodelist
while(allOpts.length > 1) {
var name = allOpts[1].attributes[0].value;
opts[name] = opts[name] || document.createDocumentFragment();
opts[name].appendChild(allOpts[1]);
}
product.addEventListener('change', function(evt) {
var val = evt.target.value;
power.innerHTML = '';
power.appendChild(opts.empty);
power.appendChild(opts[val].cloneNode(true));
});
更新
只需一點點更多的工作,這可以更通用的 - 說,當新的需求進來註釋到原來的答案:)
附加要求
注意:我添加了空白選項元素以使解決方案更簡單。代碼也可以適用於處理它的缺失,但這將作爲讀者的練習。
<select name="continent" id="continent">'
<option value="IMAGES/maps/europe_gray.png" id="EU">Europe</option>
<option value="IMAGES/maps/europe_gray.png" id="AS">Asia</option>
</select>
<select name="country" id="country">
<option></option>
<option name="EU" id="ALB" value="IMAGES/flags/al.png">Albania, Republic of</option>
<option name="AS" id="RUS" value="IMAGES/flags/ru.png">Russia</option>
</select>
與下面的變化,控制器邏輯是現在,因爲它可以通過接受,告訴它在哪裏可以找到其鍵和選擇元素的其他信息適應標記更通用。
Generic Controller Example
/* params {
* master: id of the master select element
* subord: id of the subordinate select element
* mKey: name of the attribute holding the key in master that will be used to filter subordinate options
* sKey: name of the attribute in subordinate that will be used to match against mKey
*/
function selectController(params) {
var master = document.getElementById(params.master);
var subord = document.getElementById(params.subord);
var allOpts = subord.getElementsByTagName('option');
var opts = {
empty: allOpts[0]
};
// Appending to the doc fragment removes it from the live nodelist
while (allOpts.length > 1) {
var name = allOpts[1].getAttribute(params.sKey);
opts[name] = opts[name] || document.createDocumentFragment();
opts[name].appendChild(allOpts[1]);
}
master.addEventListener('change', function (evt) {
var sel = master.options[master.selectedIndex];
var mKey = sel.getAttribute(params.mKey);
subord.innerHTML = '';
subord.appendChild(opts.empty);
subord.appendChild(opts[mKey].cloneNode(true));
});
}
selectController({
master: 'product',
subord: 'power',
mKey: 'id',
sKey: 'name'
});
selectController({
master: 'continent',
subord: 'country',
mKey: 'id',
sKey: 'name'
});
來源
2013-08-20 06:30:51
dc5
剛再生與您需要的時候'product'變化值列表。 – Bart