[英]Adding an Option to a Select Element with javascript using the Prototype library
我想使用 Prototype 向 Select 元素动态添加一个选项。
似乎有很多不同的方法可以做到这一点,都与 options.add 等有关......在跨浏览器的方式中看不到太多。
希望它尽可能轻巧。
这就是我到目前为止所得到的。 这只是附加我坚持的选项:
var oNewOption = new Element('option').value=vItem;
oNewOption.text=vItem;
有什么想法吗?
不需要 Prototype,使用自 1990 年代中期以来适用于所有主要桌面浏览器的以下历史悠久的方法同样容易:
// Assuming a select element stored in a variable called 'select'
select.options[select.options.length] = new Option("Option text", "optionValue");
select.insert(new Element('option', {value: myValue}).update(myLabel));
insert追加到select对象的内容, update更新新选项对象的内容。
不过,并不比经典方式更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.