这里没有尝试跨浏览器......但我能想到的只有一个浏览器可能会给你带来麻烦:)
演示
代码
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 的版本:
演示
文档片段版本
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>
通过以下更改,控制器逻辑现在更加通用,因为它可以通过接受告诉它在哪里找到其键和选择元素的附加信息来适应标记。
通用控制器示例
/* 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'
});