jQuery 如何使用jQuery检查复选框?

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

How to make a checkbox checked with jQuery?

jqueryhtmlcheckbox

提问by Earlz

Possible Duplicate:
How do I check a checkbox with JQuery or Javascript?

可能重复:
如何使用 JQuery 或 Javascript 选中复选框?

I'm trying to make a checkbox checked (or not) with jQuery.

我正在尝试使用 jQuery 选中(或不选中)复选框。

My example HTML:

我的示例 HTML:

<input type="checkbox" id="test" name="test" />

Attempt at clearing a checkbox(doesn't work)

尝试清除复选框(不起作用)

$('#test').val('off');

and at checking:

并在检查:

$('#test').val('on');

How do I control checkboxes with jQuery?

如何使用 jQuery 控制复选框?

回答by Dunhamzzz

$('#test').prop('checked', true);

Note only in jQuery 1.6+

仅在 jQuery 1.6+ 中注意

回答by ShankarSangoli

$('#test').attr('checked','checked');

$('#test').removeAttr('checked');

回答by Jason Kaczmarsky

$('#checkbox').prop('checked', true);

When you want it unchecked:

当您希望取消选中它时:

$('#checkbox').prop('checked', false);

回答by Nicola Peluchetti

I think you should use prop(), if you are using jQuery 1.6 onwards.

我认为你应该使用 prop(),如果你使用 jQuery 1.6 以后。

To check it you should do:

要检查它,您应该执行以下操作:

$('#test').prop('checked', true);

to uncheck it:

取消选中它:

$('#test').prop('checked', false);

回答by diEcho

from jQuery v1.6 use prop

从 jQuery v1.6 使用prop

to check that is checkd or not

检查是否被检查

$('input:radio').prop('checked') // will return true or false

and to make it checkd use

并使其检查使用

$("input").prop("checked", true);

回答by N0ug4t

You don't need to control your checkBoxes with jQuery. You can do it with some simple JavaScript.

您不需要使用 jQuery 控制复选框。您可以使用一些简单的 JavaScript 来实现。

This JS snippet should work fine:

这个 JS 片段应该可以正常工作:

document.TheFormHere.test.Value = true;

document.TheFormHere.test.Value = true;