jQuery 1.9 以后的 .attr(“checked”, true)
jQuery 1.9 以后, 使用 .attr(“checked”,true) 或 attr(“checked”, “checked”) 将无法正确设置 checkbox的状态, 同样的, 使用 .attr(“checked”) 也无法正确获取checkbox的状态
新版本开始 使用 .prop(“checked”, true) 和 .prop(“checkbox”) 来设置和获取checkbox的勾选状态,。 您可能更习惯使用 .is(“:checked”) 来获取状态,
发现第一次使用 .attr(“checked”, true) 的时候, checkbox被正确的勾选上了, 但是如果取消了勾选状态(手动取消,.removeAttr(“checked”), 或.attr(“checked”,false)中的任何一种方法), 再次使用.attr(“checked”,true) 或 .attr(“checked”, “checked”) 都将没有任何效果.
所以, 请使用 .prop(“checked”, true) 来设置状态, 使用 .prop(“checked”) 来获取状态