Javascript jQuery取消选中复选框不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11066436/
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
jQuery Uncheck checkbox not working
提问by stoopkid1
After trying multiple examples on StackOverflow to fix my issue, I still can't figure out my problem.
在 StackOverflow 上尝试了多个示例来解决我的问题后,我仍然无法弄清楚我的问题。
I have 3 checkboxes:
我有 3 个复选框:
<input id="pets_dog" class="text" type="checkbox" class="pets" name="pets[]" value="pets_dog"> Dogs</input>
<input id="pets_cats" class="text" type="checkbox" class="pets" name="pets[]" value="pets_cats"> Cats</input>
<input id="pets_none" class="text" type="checkbox" class="pets" name="pets[]" value="pets_no"> None</input>
And when "pets_none" is checked, I need the other 2 checkboxes to be unchecked and vice versa. Here is my current jQuery code:
检查“PES_NONE”时,我需要未选中其他2个复选框,反之亦然。这是我当前的 jQuery 代码:
$("#pets_none").change(function() {
if($("#pets_none").attr('checked')) {
$("#pets_cats").removeAttr('checked');
$("#pets_dogs").removeAttr('checked');
}
});
I can't figure out for the life of me why it's not working, and appreciate any help.
我一生都无法弄清楚为什么它不起作用,并感谢任何帮助。
回答by The Alpha
Your html should be
你的 html 应该是
<input id="pets_dog" class="text pets" type="checkbox" name="pets[]" value="pets_dog" /> Dogs
<input id="pets_cats" class="text pets" type="checkbox" name="pets[]" value="pets_cats" /> Cats
<input id="pets_none" class="text pets" type="checkbox" name="pets[]" value="pets_no" /> None???????????
JS
JS
$(function(){
$("#pets_none").on('change', function(e) {
if($(this).is(':checked')) {
$("#pets_dog").attr('checked', false);
$("#pets_cats").attr('checked', false);
}
});
});
Update :You have used class attribute twice, it should be as follows
更新:您已经使用过两次类属性,应该如下
<input id="pets_dog" type="checkbox" class="pets text" name="pets[]" value="pets_dog" /> Dogs
<input id="pets_cats" type="checkbox" class="pets text" name="pets[]" value="pets_cats" /> Cats
<input id="pets_none" type="checkbox" class="text pets" name="pets[]" value="pets_no" /> None?
JS
JS
$(function(){
$(".pets").on('change', function(e) {
var el=$(this);
if(el.is(':checked') && el.attr('id')=='pets_none') {
$(".pets").not(el).attr('checked', false);
}
else if(el.is(':checked') && el.attr('id')!='pets_none')
{
$("#pets_none").attr('checked', false);
}
});
});
回答by undefined
You can use the prop
method:
您可以使用以下prop
方法:
$("#pets_none").change(function() {
if (this.checked) {
$("#pets_cats, #pets_dog").prop('checked', false);
}
});
回答by gvl
Use the .prop()
method:
使用.prop()
方法:
...
$("#pets_cats").prop('checked', false);
$("#pets_dogs").prop('checked', false);
...
And to check whether the #pets_none
is checked, use that method too:
并检查是否#pets_none
被选中,也使用该方法:
$("#pets_none").prop('checked');
回答by wonna
var checked = $(this).find('Enable').text().toLowerCase();
$("#chk__Enable").each(function () {
if (checked == 'true') {
$(this).attr('checked', 'checked');
} else {
$(this).removeAttr('checked');
}
});