单击div选中/取消选中复选框 [英] Click a div to check / uncheck a checkbox

查看:135
本文介绍了单击div选中/取消选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应用了以下代码来使表行在单击时选中/取消选中子复选框。现在,我发现单击行中的复选框本身不会进行检查。可能是它检查click(标准功能),然后jquery拾取click事件并取消选中它吗?我该如何解决?

I applied the following code to make table rows check/uncheck a child checkbox when clicked. Now I discovered that when clicking the checkbox itself inside the row it doesent check. Could it be that it checks on click (standard function) and then the jquery picks up the click event and unchecks it? How can I fix this?

//check on div click
$("tr").live("click",function() {
    var checkbox = $(this).find("input[type='checkbox']");

    if( checkbox.attr("checked") == "" ){
        checkbox.attr("checked","true");
    } else {
        checkbox.attr("checked","");
    }
});


推荐答案

我怀疑click事件正在从复选框中冒泡到tr。尝试同时添加以下代码:

I suspect that the click event is bubbling from the checkbox to the tr. Try adding this code as well:

$('tr input[type=checkbox]').click(function(e){
        e.stopPropagation();
});

编辑

这是一个示例: http://jsfiddle.net/GSqNv/

这篇关于单击div选中/取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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