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
how to unselect all options in multiselect widget
提问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]').length
is 1 and ui.checked
and calling the uncheckAll
but after that it is behaving properly the code
if $this.children("option").not('[selected]').length
gives 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 getChecked
to 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");