jQuery jquery删除选项的“selected”属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7960773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:31:49  来源:igfitidea点击:

jquery remove "selected" attribute of option?

jqueryinternet-explorer

提问by Don Zacharias

Why doesn't this work in IE8 to deselect all options in a multiline selectbox?

为什么这不能在 IE8 中取消选择多行选择框中的所有选项?

$("#myselect").children().removeAttr("selected");

Is there a workaround? Nothing I could think of (attr("selected","") etc) seems to work.

有解决方法吗?我想不到的任何东西(attr(“selected”,“”)等)似乎都有效。

UPDATE: Here is an updated jsFiddle.I have at leastgot it to degrade so that in IE8 the first option is selected. But without the hardcoded selected='selected' andthe .attr call that IE8 seems to need, it does 3 different things in Firefox, Chrome and IE8! See this version:, which is simple and seems like it should work:

更新:这是一个更新的 jsFiddle。至少让它降级,以便在 IE8 中选择第一个选项。但是没有硬编码的 selected='selected'IE8 似乎需要的 .attr 调用,它在 Firefox、Chrome 和 IE8 中做了 3 种不同的事情!请参阅此版本:,它很简单,看起来应该可以工作:

  • in Firefox: nothing selected
  • in Chrome: 0th option selected
  • in IE8: 1st option selected
  • 在 Firefox 中:未选择任何内容
  • 在 Chrome 中:选择了第 0 个选项
  • 在 IE8 中:选择了第一个选项

Maybe I have made myself crazy and there is a mistake in there somewhere I can't see?

也许我让自己发疯了,我看不到的地方有错误?

采纳答案by radiak

It's something in the way jQuery translates to IE8, not necessarily the browser itself.

这是 jQuery 转换为 IE8 的方式,不一定是浏览器本身。

I was able to work around by going old school and breaking out of jQuery for one line:

我能够通过去老派和突破 jQuery 一行来解决:

document.getElementById('myselect').selectedIndex = -1;

回答by Jon

The question is asked in a misleading manner. "Removing the selectedattribute" and "deselecting all options" are entirely different things.

这个问题是以一种误导的方式提出的。“删除selected属性”和“取消选择所有选项”是完全不同的事情

To deselect all options in a documented, cross-browser manner use either

要以文档化的跨浏览器方式取消选择所有选项,请使用

$("select").val([]);

or

或者

// Note the use of .prop instead of .attr
$("select option").prop("selected", false);

回答by Gabe

This works:

这有效:

$("#myselect").find('option').removeAttr("selected");

or

或者

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

jsFiddle

js小提琴

回答by Santosh

Using jQuery 1.9 and above:

使用 jQuery 1.9 及更高版本:

$("#mySelect :selected").prop('selected', false);

回答by JJ Zabkar

Similar to @radiak's response, but with jQuery (see this API documentand comment on how to change the selectedIndex).

类似于@radiak 的响应,但使用 jQuery(请参阅此 API 文档并评论如何更改selectedIndex)。

$('#mySelectParent').find("select").prop("selectedIndex",-1);

$('#mySelectParent').find("select").prop("selectedIndex",-1);

The benefits to this approach are:

这种方法的好处是:

  • You can stay within jQuery
  • You can limit the scope using jQuery selectors (#mySelectParentin the example)
  • More explicitly defined code
  • Works in IE8, Chrome, FF
  • 你可以留在 jQuery
  • 您可以使用 jQuery 选择器来限制范围(#mySelectParent在示例中)
  • 更明确定义的代码
  • 适用于 IE8、Chrome、FF

回答by Eolia

Another alternative:

另一种选择:

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

Hope it helps

希望能帮助到你

回答by Pierre

Well, I spent a lot of time on this issue. To get an answer working with Chrome AND IE, I had to change my approach. The idea is to avoid removing the selected option (because cannot remove it properly with IE). => this implies to select option not by adding or setting the selected attribute on the option, but to choose an option at the "select" level using the selectedIndex prop.

嗯,我在这个问题上花了很多时间。为了得到 Chrome 和 IE 的答案,我不得不改变我的方法。这个想法是为了避免删除选定的选项(因为无法使用 IE 正确删除它)。=> 这意味着不是通过在选项上添加或设置 selected 属性来选择选项,而是使用 selectedIndex 属性在“选择”级别选择一个选项。

Before :

前 :

$('#myselect option:contains("value")').attr('selected','selected');
$('#myselect option:contains("value")').removeAttr('selected'); => KO with IE

After :

后 :

$('#myselect').prop('selectedIndex', $('#myselect option:contains("value")').index());
$('#myselect').prop('selectedIndex','-1'); => OK with all browsers

Hope it will help

希望它会有所帮助