简体   繁体   English

jQuery:选择元素中的取消选择选项

[英]jQuery: unselect option in select element

I faced with a strange behaviour of select element. 我面临选择元素的奇怪行为。 So, I have a select element with several options. 因此,我有一个带有几个选项的select元素。 One of option is empty - it's required by plugin to output placeholder. 选项之一为空-插件要求它输出占位符。

I needed functionality that would clear selected options and I wrote something like: 我需要清除所选选项的功能,并且编写了如下内容:

$(element).val('');
$(element).find("option:selected").removeAttr("selected");

The thing is that "selected" attribute is still here and it's on old option - you can see it in the code sample. 关键是“ selected”属性仍然在这里,并且在旧选项上-您可以在代码示例中看到它。

So, I have 2 questions: 所以,我有两个问题:

1) Why .val() method of jQuery library do not update "selected" attribute in options list? 1)为什么jQuery库的.val()方法不更新选项列表中的“ selected”属性?

2) Why I can not update "selected" attribute in my case? 2)为什么我不能更新“ selected”属性? If I switch these statements it's working: 如果我切换这些语句,它将起作用:

$(element).find("option:selected").removeAttr("selected");
$(element).val('');

Code sample: 代码示例:

 $(function(){ $("#unselect").click(function(){ $("#lang_type").val(''); $("#lang_type").find("option:selected").removeAttr("selected"); alert($("#lang_type").html()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="lang_type"> <option value=""></option> <option value="01">01 - Language of text</option> <option value="02">02 - Original language of a translated text</option> <option selected="selected" value="03">03 - Language of abstracts</option> <option value="04">04 - Rights language</option> <option value="05">05 - Rights-excluded language</option> <option value="06">06 - Original language in a multilingual edition</option> <option value="07">07 - Translated language in a multilingual edition</option> <option value="08">08 - Language of audio track</option> <option value="09">09 - Language of subtitles</option> </select> <button id="unselect">Unselect</button> 

EDIT: You can use prop(false) property like this 编辑:您可以像这样使用prop(false)属性

$(function(){
  $("#unselect").click(function(){    
    $("#lang_type").val('');
    $("#lang_type").find("option:selected").prop('selected',false);
  });
});

Like @yezzz said, read this : 就像@yezzz所说的那样,请阅读:
Note: Do not use removeProp() method to remove native properties such as checked, disabled, or selected. 注意:不要使用removeProp()方法来删除本机属性,例如选中,禁用或选定。 This will remove the property completely and, once removed, cannot be added again to element. 这将完全删除该属性,并且一旦删除,就不能再次将其添加到element中。 Use .prop() to set these properties to false instead. 使用.prop()将这些属性设置为false。

If I'm not mistaken, a multi-select can be initially unselected, but once any option is selected, it can not be unselected any more. 如果我没记错的话,最初可以取消选择多选,但是一旦选择了任何选项,就不能再取消选择了。 RFC 1866 states in section 8.1.3: RFC 1866在第8.1.3节中规定:

The initial state has the first option selected, unless a SELECTED attribute is present on any of the elements. 除非选中任何元素,否则初始状态将选择第一个选项。

This lets me to believe that one option MUST always be selected. 这使我相信必须始终选择一个选项。 Obviously, different browsers interpret this differently... 显然,不同的浏览器对此解释不同。

But it does not seem to be a jQuery issue, rather a browser implementation issue. 但这似乎不是jQuery问题,而是浏览器实现问题。

The selected attribute reflects merely the initial state of the select input. selected属性仅反映选定输入的初始状态。 You shouldn't really care about removing it, as it affects nothing once a different option is selected (either by the user or by a script on your page). 您不必在意删除它,因为一旦选择了其他选项(由用户或页面上的脚本),它就不会受到影响。

The current state of the input can be read or modified via the selectedIndex property, where a value of -1 means no option is selected (which never is the default, as there always is an option selected initially). 可以通过selectedIndex属性读取或修改输入的当前状态,其中值-1表示未选择任何选项(这永远不是默认值,因为最初总是选择了一个选项)。 However, you seem to want to select a particular "empty" option. 但是,您似乎想选择一个特定的“空”选项。 Setting the value on a select box results in the corresponding option being selected, which, in your case, is the very first one. 在选择框中设置值会导致选择相应的选项,在您的情况下,这是第一个选项。

The code probably does exactly what you want . 该代码可能完全符合您的要求 So don't mind checking the HTML, as the selected attribute - again - is unrelated to the current state of the input. 因此,不必介意检查HTML,因为selected属性(再次)与输入的当前状态无关。

The :selected selector, however, matches the elements that are currently selected. 但是, :selected选择器匹配当前选定的元素。 Your first snippet selects an option, thus making it :selected , then attempts to remove a non-existent attribute from it. 您的第一个代码片段选择一个选项,使其成为:selected ,然后尝试从中删除不存在的属性。 The second snippet of yours assumes that the selection remains on the option that was initially selected, and then removes the attribute from it. 您的第二个片段假定所选内容保留在最初选择的选项上,然后从中删除该属性。 What follows is the "empty" option getting selected, and no more steps need to be taken, as that's all it takes to select an option. 接下来是选择“空”选项,无需采取任何其他步骤,因为这是选择一个选项所需要的全部。

To summarize: you can safely drop all the code that deals with the removal of the selected attribute, as it doesn't affect the current state of the element, the state being already tied to the correct option. 总结一下:您可以放心地删除所有与删除selected属性有关的代码,因为它不会影响元素的当前状态,该状态已经绑定到正确的选项。

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

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