Select 选项未在 IE9 上动态设置

Select option not dynamically set on IE9

我看了所有与此相关的答案,但还是想不通。请帮忙。 我有一个 jquery 代码如下:

$('#DDPrimaryServiceLine').find('option:contains("' + rowData['Primary_Service_Line'] + '")').attr("selected", true);

rowData 是通过数据库调用从 ajax 生成的。同样最初 select 是空的,它在 运行 时动态填充。默认情况下它有 null 作为 selected.

在 运行ning 上,下拉列表在 IE9 中显示默认 selected null,尽管它在 Google Chrome 中运行良好。

求推荐。 (我试着写

$("#DDPrimaryServiceLine option:selected").removeAttr('selected');

也在我的代码中。但不起作用 )

这里的解决方案有帮助吗Best way to unselect a <select> in jQuery?

使用 .prop("selected", false) 代替 .removeAttr() ?

我还需要查看您的 html 代码以查看错误可能出在哪里。检查此 link: ie9 select default value

评论作为答案:

您应该设置 selectval() 而不是选择 option,但是如果您想更改 [=13] 的选定 属性 =],使用 prop() 而不是 attr()。这会设置正确的内部值(并触发选择更改),而不仅仅是更改显示属性。

例如

$('#DDPrimaryServiceLine').find('option:contains("' + rowData['Primary_Service_Line'] + '")').prop("selected", true);

prop 具有额外的优势,即值 returned 不仅仅是字符串,它会正确地 return truefalse boolean 值,如果属性是 "true" 或 "false".