如何防止默认的复选框事件覆盖我的jQuery检查/取消选中功能? [英] How do I prevent default checkbox event from overriding my jQuery check/uncheck function?

查看:166
本文介绍了如何防止默认的复选框事件覆盖我的jQuery检查/取消选中功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表的复选框里面的表与一个简单的jQuery函数,允许用户单击表格行中的任何地方,以检查/取消选中该复选框。它工作伟大,除非当用户实际上点击复选框。它不工作。有任何想法吗?这是我的代码:

I have a list of checkboxes inside a table with a simple jQuery function that allows the user to click anywhere in the table row to check/uncheck the checkbox. It works great, except when the user actually clicks on the checkbox. It doesn't work then. Any ideas? Here is my code:

HTML:

<tr onClick="checkBox()">...</tr>

jQuery:

function checkBox() {

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

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

return false;
}


推荐答案

=http://api.jquery.com/event.stopPropagation/> event.stopPropagation 以避免您的输入#myContacts 的点击事件传播到 tr 的点击事件。此外,我不能相信你是以这种方式混合jQuery和DOM,当你应该使用jQuery事件。

You can use event.stopPropagation to avoid your input#myContacts's click event propagates to the tr's click event. Also, I can't believe you are mixing jQuery and DOM in that way yet, when you should be using jQuery Events.

$(document).ready(function(){
  $("input#myContacts").click(function(e){
    e.stopPropagation();
  });
});

这篇关于如何防止默认的复选框事件覆盖我的jQuery检查/取消选中功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆