How to make a checkbox checked with jQuery?

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

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

My example 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?


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

仅在jQuery 1.6+中注意


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

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

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

To check it you should do:

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

to uncheck it:

$('#test').prop('checked', false);
链接地址: http://www.djcxy.com/p/12250.html

上一篇: jQuery复选框选中/取消选中

下一篇: 如何使用jQuery检查复选框?