Javascript 如何使用jquery取消选择选择中的选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39245967/
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-23 22:21:48  来源:igfitidea点击:

How to unselect options in select using jquery

javascriptjqueryhtml

提问by Akhil Ghosh

Below is my HTML. I have given multiple optionelements in the selecttag.

下面是我的 HTML。我optionselect标签中给出了多个元素。

<select class="car" multiple size="3">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
</select>

I am able to multi-select by holding the CTRLkey. I am able to retrieve the selected values using the following jQuery

我可以通过按住CTRL键进行多选。我能够使用以下 jQuery 检索选定的值

$.each($(".car option:selected"), function() {
    countries.push($(this).val());
});

How can I unselect the value using jQuery? The selected value will be highlighted as shown:

如何使用 jQuery 取消选择值?所选值将突出显示,如下所示:

enter image description here

在此处输入图片说明

Thanks in advance

提前致谢

回答by beerwin

Set the selectedproperty to false: $(selector).prop('selected', false).

selected属性设置为false: $(selector).prop('selected', false)

I have added a button and attached a click event to be able to demonstrate.

我添加了一个按钮并附加了一个单击事件以进行演示。

var countries = [];

function unselect() {
    $.each($(".car option:selected"), function () {
        countries.push($(this).val());
        $(this).prop('selected', false); // <-- HERE
    });
}

$("#unselect").click(unselect);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select class="car" multiple size="3">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
</select>

<input type="button" value="unselect" id="unselect" />

回答by Haresh Vidja

You can use $('.car').val([]);for unselect all options in multi-select dropdown.

您可以$('.car').val([]);用于取消选择多选下拉列表中的所有选项。

For multi select value you can pass empty array for unselect all options.

对于多选值,您可以传递空数组以取消选择所有选项。

$(document).ready(function(){
  $("#select").click(function(){
    var values= [];
    $(".car > option").each(function(){
     values.push($(this).attr('value'));
    });
    $('.car').val(values);
  });
  
  $("#unselect").click(function(){
    $('.car').val([]);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 <select class="car" multiple size="3">
  <option value="volvo" selected>Volvo</option>
  <option value="saab" selected>Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  </select>
  <button id="select">Select All</button>
  <button id="unselect">Unselect All</button>
  </button>