如何防止默认复选框事件覆盖我的jQuery检查/取消检查功能?

Joh*_*son 6 checkbox jquery

我在表格中有一个复选框列表,其中包含一个简单的jQuery函数,允许用户单击表格行中的任意位置来选中/取消选中该复选框.它工作得很好,除非用户实际点击了复选框.它不起作用.有任何想法吗?这是我的代码:

HTML:

<tr onClick="checkBox()">...</tr>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

function checkBox() {

var ischecked = $('input#myContacts').attr("checked");

if(ischecked)
{
    $('input#myContacts').attr("checked", false);
}
else
{
    $('input#myContacts').attr("checked", true);
}

return false;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*der 8

您可以使用event.stopPropagation以避免您input#myContacts的点击事件传播到tr点击事件.另外,当你应该使用jQuery Events时,我无法相信你正在以这种方式混合使用jQuery和DOM.

$(document).ready(function(){
  $("input#myContacts").click(function(e){
    e.stopPropagation();
  });
});
Run Code Online (Sandbox Code Playgroud)