简体   繁体   English

如何使用原型js附加一堆选项以选择HTML中的标签

[英]How to append bunch of option to select tag in HTML using prototype js

How to append bunch of option to select tag in HTML using prototype js. 如何使用原型js附加一堆选项以选择HTML中的标签。 Thank you. 谢谢。

With a reference to the select tag: 参考select标签:

var select = $('my_select_tag_id');

Iterate over your options in a hash: 遍历哈希中的选项:

$H({foo: 'bar', boo: 'baz'}).each(function(pair){
  select.options[select.options.length] = new Option(pair.value, pair.key);
});

It is important that you don't try to just shove the values into the option as if they were HTML elements, using select.insert('<option value="foo">bar</option>') or similar, because that won't work in all browsers. 重要的是,不要试图使用select.insert('<option value="foo">bar</option>')或类似方法将值当作HTML元素推入选项中,因为那样不适用于所有浏览器。 jQuery does something fancy to work around this, because that's precisely the way they would have you do it. jQuery可以解决此问题,因为这正是他们希望您这样做的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM