我正在尝试实施一个简单的检查所有选项。单击复选框,应选中页面中的所有复选框。首先尝试检查主复选框的值
以下代码在Firefox中运行良好,但在IE8中运行不正常。如何解决问题
function checkAll()
{
alert("value of checkbox = "+ $('#mastercheckbox').attr('checked') )
}
和html代码
<input type="checkbox" id="mastercheckbox" onchange="checkAll();"/>
答案 0 :(得分:1)
查看prop()
:http://api.jquery.com/prop/
文档提供了多种更好的方法来检查“已选中”复选框。
elem.checked | true (Boolean) Will change with checkbox state
$(elem).prop("checked") | true (Boolean) Will change with checkbox state
elem.getAttribute("checked") | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6) | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+) | "checked" (String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6) | true (Boolean) Changed with checkbox state