jQuery 如何取消选择多选小部件中的所有选项

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

how to unselect all options in multiselect widget

jquerymulti-select

提问by coool

I am using jquery multiselect widget. I would like to uncheck all if all them were checked manually. I have a sample code in JSFiddle--> Code

我正在使用 jquery 多选小部件。如果所有这些都是手动检查的,我想取消全部选中。我在JSFiddle-->代码中有一个示例代码

This doesn't work. When the select options are selected manually, check for no of unchecked options in select it gives me 1 (at the click of last option) the first time. so I checked if $this.children("option").not('[selected]').lengthis 1 and ui.checkedand calling the uncheckAllbut after that it is behaving properly the code if $this.children("option").not('[selected]').lengthgives me 0 when selecting the final one.

这不起作用。当手动选择选择选项时,检查选择中没有未选中的选项它第一次给我 1(点击最后一个选项)。所以我检查了是否$this.children("option").not('[selected]').length为 1 并 ui.checked调用了uncheckAll但之后它的行为正常,如果$this.children("option").not('[selected]').length在选择最后一个时给我 0。

I am wondering what is happening.

我想知道发生了什么。

回答by dku.rajkumar

well so here is the solution what you want... check the documentation, there is method getCheckedto get selected options. yours is not working because the pluging reformat the select elements.

那么这里是您想要的解决方案......检查文档,有方法getChecked可以获取selected options。你的不起作用,因为插件重新格式化了选择元素。

$('.multi').multiselect({
    click: function(e, ui){
    if($(this).multiselect("getChecked").length  
                              == $('select.multi > option').length){
       $(this).multiselect("uncheckAll");    
    }        
  }
});

fiddle example :http://jsfiddle.net/fG6PT/11/

小提琴示例:http : //jsfiddle.net/fG6PT/11/

回答by Pramod

$("#multiselectelement").multiselect("uncheckAll");