我看你用了jquery1.9.1。
建议用prop属性
prop修改dom属性。
$(el).prop('checked', true); 等于
el.checked = true;
attr修改html标签的属性。
$(el).prop('checked', true); 等于
el.setAttribute('checked', 'checked');
修改html标签的属性时有的属性不能呈现在浏览器上。
$("document").ready(function () {
$("#btn1").click(function () {
$("[name='checkbox']").prop("checked", true); //全选
})
$("#btn2").click(function () {
$("[name='checkbox']").prop("checked", false); //取消全选
})
$("#btn3").click(function () {
$("[name='checkbox']:even").prop("checked", true); //选中所有奇数
})
$("#btn4").click(function () {
$("[name='checkbox']").each(function () { //反选
if ($(this).prop("checked")) {
$(this).prop("checked", false);
} else {
$(this).prop("checked", true);
}
})
})
$("#btn5").click(function () {
var str = "";
// 使用:checked选择器
$("[name='checkbox']:checked").each(function () {
str += $(this).val() + "\n";
//alert($(this).val());
})
alert(str);
})
})
只执行一次?你点一下 执行一次吧。