jquery 表格行选一行选中checkbox,但首次点击checkbox要点两次才能选中
问题描述:
这是js 行选的代码段,可以行选,但是点击checkbox本身要点鼠标两次才能选中,求原因。
// 行选中
$("table tbody tr").click(function() {
var input = $(this).find("input[type=checkbox]");//获取checkbox
//给checkbox的click添加阻止冒泡事件
input.click(function(event){
event.stopPropagation();
});
//判断当前checkbox是否为选中状态
if(input.prop("checked")){
input.prop("checked",false);
} else{
input.prop("checked",true);
}
});
答
prop只是获取在匹配的元素集中的第一个元素的属性值,不应该作为if的判断条件,使用is
/* 全选 */
$("#checkedAll").click(function() {
if ($(this).is(":checked") == true) { // 全选
$("input[name='ids']").each(function() {
$(this).attr("checked", true);
});
} else { // 取消全选
$("input[name='ids']").each(function() {
$(this).attr("checked", false);
});
}
});