Internet Explorer 不知道如何将选项标签添加到 jQuery 中的选择
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 id 为“category”的元素实际上是一个
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:尝试
或
try
or
这个解决方案在 IE8 下运行良好 - 从微软论坛复制 -
“我假设你已经得到了你需要的答案,但是对于在搜索这个问题时找到这篇文章的其他人(就像我一样),这里是对我有用的解决方案。所要做的就是在将 opt 添加到选项集合后设置属性,我还发现 MSDN 的 add 函数(针对选项集合)页面明确指出,对于 IE,必须在添加选项后设置属性,但我在网上找到的大多数例子都不是这样做的,我认为你的方法可能在旧版本的 IE 中有效。”
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."
add()
调用不应在select
元素上进行,而不是在选项集合上进行吗? IE:Should the
add()
call not be on theselect
element, rather than the collection of options? I.e.: