For jQuery 1.6+ :
(对于jQuery 1.6+:)
.attr() is deprecated for properties;
(.attr()已弃用于属性;)
use the new .prop() function instead as: (使用新的.prop()函数代替:)
$('#myCheckbox').prop('checked', true); // Checks it
$('#myCheckbox').prop('checked', false); // Unchecks it
For jQuery < 1.6:
(对于jQuery <1.6:)
To check/uncheck a checkbox, use the attribute checked
and alter that.
(要检查/取消选中复选框,使用属性checked
和改变这一点。)
With jQuery you can do: (使用jQuery,您可以:)
$('#myCheckbox').attr('checked', true); // Checks it
$('#myCheckbox').attr('checked', false); // Unchecks it
Cause you know, in HTML, it would look something like:
(因为你知道,在HTML中,它看起来像:)
<input type="checkbox" id="myCheckbox" checked="checked" /> <!-- Checked -->
<input type="checkbox" id="myCheckbox" /> <!-- Unchecked -->
However, you cannot trust the .attr() method to get the value of the checkbox (if you need to).
(但是,您无法信任.attr()方法来获取复选框的值(如果需要)。)
You will have to rely in the .prop() method. (您将不得不依赖.prop()方法。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…