Javascript 如何使用 jQuery 或纯 JS 重置所有复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2279760/
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 reset all checkboxes using jQuery or pure JS?
提问by streetparade
How can I reset all checkboxes in a document using jQuery or pure JS?
如何使用 jQuery 或纯 JS 重置文档中的所有复选框?
回答by Tatu Ulmanen
If you mean how to remove the 'checked' state from all checkboxes:
如果您的意思是如何从所有复选框中删除“已选中”状态:
$('input:checkbox').removeAttr('checked');
回答by Kevin Q. Yu
If you want to use form's reset feature, you'd better to use this:
如果你想使用表单的重置功能,你最好使用这个:
$('input[type=checkbox]').prop('checked',true);
OR
或者
$('input[type=checkbox]').prop('checked',false);
Looks like removeAttr()can not be reset by form.reset().
貌似removeAttr()不能重置form.reset()。
回答by Sundeep
The above answer did not work for me -
上面的答案对我不起作用 -
The following worked
以下工作
$('input[type=checkbox]').each(function()
{
this.checked = false;
});
This makes sure all the checkboxes are unchecked.
这可确保取消选中所有复选框。
回答by Dean Burge
In some cases the checkbox may be selected by default. If you want to restore default selection rather than set as unselected, compare the defaultCheckedproperty.
在某些情况下,默认情况下可能会选中该复选框。如果要恢复默认选择而不是设置为未选择,请比较defaultChecked属性。
$(':checkbox').each(function(i,item){
this.checked = item.defaultChecked;
});
回答by jef
I have used this before:
我以前用过这个:
$('input[type=checkbox]').prop('checked', false);
seems that .attr and .removeAttr doesn't work for some situations.
似乎 .attr 和 .removeAttr 在某些情况下不起作用。
edit: Note that in jQuery v1.6 and higher, you should be using .prop('checked', false)instead for greater cross-browser compatibility - see https://api.jquery.com/prop
编辑:请注意,在 jQuery v1.6 及更高版本中,您应该.prop('checked', false)改用以获得更大的跨浏览器兼容性 - 请参阅https://api.jquery.com/prop
Comment here: How to reset all checkboxes using jQuery or pure JS?
回答by kenorb
Javascript
Javascript
var clist = document.getElementsByTagName("input");
for (var i = 0; i < clist.length; ++i) { clist[i].checked = false; }
jQuery
jQuery
$('input:checkbox').each(function() { this.checked = false; });
To do opposite, see: Select All Checkboxes By ID/Class
要进行相反的操作,请参阅:按 ID/类别选择所有复选框
回答by Yene Mulatu
$(":checkbox:checked").each(function () {
this.click();
});
to unchecked checked box, turn your logic around to do opposite
取消选中的复选框,将您的逻辑转过来做相反的事情
回答by kavinder
You can be selective of what div or part of your page can have checkboxes checked and which don't. I came across the scenario where I have two forms in my page and one have prefilled values(for update) and other need to be filled fresh.
您可以选择哪些 div 或页面的一部分可以选中复选框,哪些不选中。我遇到过这样一种情况,我的页面中有两个表单,一个有预填充的值(用于更新),其他需要重新填充。
$('#newFormId input[type=checkbox]').attr('checked',false);
this will make only checkboxes in form with id newFormId as unchecked.
这只会使表单中的复选框 id newFormId 未选中。
回答by Michel Ayres
As said in Tatu Ulmanen's answerusing the follow script will do the job
正如在Tatu Ulmanen 的回答中所说,使用以下脚本将完成这项工作
$('input:checkbox').removeAttr('checked');
But, as Blakomen's commentsaid, after version 1.6 it's better to use jQuery.prop()instead
但是,正如Blakomen 的评论所说,在 1.6 版之后最好jQuery.prop()改用
Note that in jQuery v1.6 and higher, you should be using .prop('checked', false) instead for greater cross-browser compatibility
请注意,在 jQuery v1.6 及更高版本中,您应该使用 .prop('checked', false) 代替以获得更大的跨浏览器兼容性
$('input:checkbox').prop('checked', false);
Be careful when using jQuery.each()it may cause performance issues. (also, avoid jQuery.find()in those case. Use each instead)
使用时要小心jQuery.each(),可能会导致性能问题。(另外,jQuery.find()在这种情况下避免。使用 each 代替)
$('input[type=checkbox]').each(function()
{
$(this).prop('checked', false);
});
回答by Andrej Gaspar
I used something like that
我用过类似的东西
$(yourSelector).find('input:checkbox').removeAttr('checked');

