Javascript 如何取消选中复选框?

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

How do I uncheck a checkbox?

javascriptjqueryhtml

提问by Jason94

I have a checkbox that is always checked, but base on user input elsewhere in my form (I have a onChange="functionName"on a select box) I would like to uncheck it.

我有一个总是被选中的复选框,但基于我表单中其他地方的用户输入(我有onChange="functionName"一个选择框),我想取消选中它。

How do I do that?

我怎么做?

回答by Arrabi

does it need to be done with jQuery ? what about JavaScript please try this:

需要用 jQuery 完成吗?JavaScript 怎么样,请试试这个:

Check: document.getElementById("ckBox").checked = true;

查看: document.getElementById("ckBox").checked = true;

UnCheck: document.getElementById("ckBox").checked = false;

取消勾选: document.getElementById("ckBox").checked = false;

回答by pixelbobby

$('#mycheckbox').attr('checked', false)

$('#mycheckbox').attr('checked', false)

回答by mcgrailm

plese see this post

请看这个帖子

also note that in jquery 1.6 you should use

还要注意,在 jquery 1.6 中你应该使用

$(".mycheckbox").prop("checked", true/false)

回答by mcgrailm

You just needs to remove the attribute checkedfrom the element.

您只需checked要从元素中删除该属性。

$("#yourSelect").change(function () {
    $("#yourCheckBox").removeAttr("checked");
});

回答by Rob Cowie

Check it:

核实:

$('input[name=foo]').attr('checked', true);

Uncheck it:

取消选中它:

$('input[name=foo]').attr('checked', false);

Adjust selector accordingly.

相应地调整选择器。

回答by josh.trow

I suppose the easiest method is to call $('theCheckbox').click()

我想最简单的方法是调用 $('theCheckbox').click()

You could also use $('theCheckbox').checked = false, or $('theCheckbox').removeAttribute('checked')

您也可以使用$('theCheckbox').checked = false, 或$('theCheckbox').removeAttribute('checked')