jQuery 如何清除复选框

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

how to clear checkbox

jquery

提问by hersh

I want to clear the selection of checkboxes if they click cancel. How can I achive this with this code:

如果单击取消,我想清除复选框的选择。如何使用此代码实现此目的:

var chkbox= $('#divListBox').find(':checked')
                 .map(function() {
                     return $(this).val();
                 }).get();

回答by Thomas Bonini

This should work:

这应该有效:

$('#divListBox :checked').removeAttr('checked');

回答by phyatt

The advantage of this solution is if you have on change events or on click events tied to the checkbox this will make them happen.

此解决方案的优点是,如果您将更改事件或单击事件绑定到复选框,这将使它们发生。

$('#divListBox').each(function(index, element) {
  var checked = element.checked;
  if (checked) {
    $(element).trigger('click');
  }
});

Also with bootstrap the checkbox wasn't getting found with the :checkedselector.

同样使用引导程序,:checked选择器没有找到复选框。

Hope that helps.

希望有帮助。

回答by Nikhil VJ

You have tagged the question with jquerybut not explicitly mentioned that you only want to use jquery, so...

您已将问题标记为jquery但未明确提及您只想使用 jquery,所以...

document.getElementById("divListBox").checked = false;

document.getElementById("divListBox").checked = false;

Frankly I only use jquery when it helps.

坦率地说,我只在 jquery 有帮助时才使用它。