Internet Explorer 不知道如何将选项标签添加到 jQuery 中的选择

发布于 2024-09-27 23:08:27 字数 319 浏览 0 评论 0原文

我在 Internet Explorer 7 和 8 中遇到了 jQuery 的问题,同时尝试向现有选择添加选项:

var s = document.getElementById("category");
s.options.add(select_option);

但 IE 只是说:

对象不支持此属性或方法

,并指向 s.options.add(select_option);

Im facing a problem with jQuery in the Internet Explorer 7 and 8, while trying to add a option to a existing select:

var s = document.getElementById("category");
s.options.add(select_option);

But IE just says:

Object doesn't support this property or method

and points to s.options.add(select_option);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末骤雨初歇 2024-10-04 23:08:27

假设 id 为“category”的元素实际上是一个

var s = document.getElementById("category");
s.options[s.options.length] = new Option("Option text", "optionValue");

Assuming the element with id "category" is actually a <select>, the easiest way is the the following time-honoured code for adding an option to a select list in any browser:

var s = document.getElementById("category");
s.options[s.options.length] = new Option("Option text", "optionValue");
优雅的叶子 2024-10-04 23:08:27

尝试

$('#category').append('<option value="foo" selected="selected">Foo</option>');

var options = $('#category').attr('options');
options[options.length] = new Option('Foo', 'foo', true, true);

try

$('#category').append('<option value="foo" selected="selected">Foo</option>');

or

var options = $('#category').attr('options');
options[options.length] = new Option('Foo', 'foo', true, true);
喜爱纠缠 2024-10-04 23:08:27

这个解决方案在 IE8 下运行良好 - 从微软论坛复制 -

“我假设你已经得到了你需要的答案,但是对于在搜索这个问题时找到这篇文章的其他人(就像我一样),这里是对我有用的解决方案。所要做的就是在将 opt 添加到选项集合后设置属性,我还发现 MSDN 的 add 函数(针对选项集合)页面明确指出,对于 IE,必须在添加选项后设置属性,但我在网上找到的大多数例子都不是这样做的,我认为你的方法可能在旧版本的 IE 中有效。”

var opt = document.createElement('option');  
select.options.add(opt);       
opt.innerHTML = 'Foo';  
opt.value = 'Bar';  

This soulution is working fine under IE8 - copied from MIcrosoft forum -

"I assume you already got the answer you needed, but for anyone else who finds this post when searching this problem (like I did), here's the solution that worked for me. All it took was setting the properties on opt AFTER adding it to the options collection. I also found that MSDN's page on the add function (for the options collection) explicitly states that for IE, the properties must be set after the option is added, but most of the examples I found online don't do it that way. I think your way may have worked in older versions of IE."

var opt = document.createElement('option');  
select.options.add(opt);       
opt.innerHTML = 'Foo';  
opt.value = 'Bar';  
不打扰别人 2024-10-04 23:08:27

add() 调用不应在 select 元素上进行,而不是在选项集合上进行吗? IE:

s.add(select_option);

Should the add() call not be on the select element, rather than the collection of options? I.e.:

s.add(select_option);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文