jQuery 如何使用jQuery删除未选中的复选框的checked="checked"?

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

How to remove checked="checked" for unchecked checkboxes with jQuery?

jquery

提问by michaelkonstincs

This is my current script

这是我当前的脚本

 <script>
        $().ready(function(){

                $('.prettycheckbox').click(function () {
                      $('.prettycheckbox input').removeAttr('checked');
                      $('.prettycheckbox a').removeClass('checked'); 

                      $("a", this).addClass('checked');
                      $("input", this).addAttr("checked");
                });

        });
 </script>

But it's not working well, bceause the part with adding and removing class to links works nice , but for input it doesn't work.

但是它工作得不好,因为添加和删除链接类的部分效果很好,但是对于输入它不起作用。

How to make the "checked" checkbox looks like this:

如何使“选中”复选框看起来像这样:

<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;" checked="checked">

and all others look like this?:

和所有其他人看起来像这样?:

<input class="payment_type" type="checkbox" value="1" name="payment_type" style="display: none;">

How to do that? Something like input radio type using jquery and checkboxes?

怎么做?类似于使用 jquery 和复选框的输入单选类型?

回答by Optimus Prime

In,

在,

jQuery 1.6+

jQuery 1.6+

To change the property of checkbox you should use the .prop()function.

要更改复选框的属性,您应该使用该.prop()功能。

$('.prettycheckbox input').prop('checked', false);
$('.prettycheckbox input').prop('checked', true);

jQuery 1.5 and below

jQuery 1.5 及以下

The .prop()function doesn't exist, but .attr()does similar:

.prop()函数不存在,但.attr()类似:

$('.prettycheckbox input').attr('checked', 'checked');

Note: removeAttris valid, but addAttris not.

注意removeAttr是有效的,但addAttr不是。

回答by Claudio Redi

Use prop()to change checkedvalue

使用prop()到的变化checked

$('.prettycheckbox input').prop('checked', false);

Something else to remark, method addAttrdoesn't exist

还有什么要说的,方法addAttr不存在

回答by shijinmon Pallikal

try this

尝试这个

document.getElementById("check1").checked = true;
document.getElementById("check1").checked = false;