jQuery TD onclick设置复选框,不冒泡 [英] jQuery td onclick to set a checkbox, no bubbling

查看:117
本文介绍了jQuery TD onclick设置复选框,不冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够单击表中的td元素,并使其选中或取消选中同一行中同级td中的复选框.

I'd like to be able to click on the td element in a table and have it check or uncheck a checkbox in a sibling td on the same row.

  • 单击复选框仍应 正常工作.
  • 单击td 复选框所在的元素 还应该选中该复选框.
  • 点击不同的td不应 需要双击(重置点击) 由于错误的切换实现
  • Clicking the checkbox should still work normally.
  • Clicking the td element the checkbox resides in should also check the checkbox.
  • Clicking different td's should not require double clicks (reset click) due to a bad toggle implementation

提琴: http://jsfiddle.net/cJ2us/

请注意,是的,这与许多问题非常相似,除非您真正了解我所遇到的问题以及那里给出的答案不适合我的问题,否则请不要链接任何重复项.例如

Please note, yes this is very similar to a number of questions, please don't link any duplicates unless you actually understand the problem I have and how the answers given there do not fit my question. e.g.

在包装的输入单选中的jQuery事件会覆盖"checked"事件 不是复选框

jQuery onclick div选中一个复选框 切换不允许点击单独的td来执行每个检查和取消检查

jQuery onclick div checks a checkbox toggle doesn't allow clicking on seperate td's to carry out each check and uncheck

在兄弟姐妹"中找到复选框< td>并使用jQuery进行检查与该示例不符,代码实际上无法按照答疑人的想法起作用

Find the Checkbox in Sibling <td> and check it using jQuery doesn't fit this example, code doesn't actually work how answerer thinks

这就是说,如果您找到合适的骗子,请告诉我!我只想要一个可行的解决方案.

In saying that, if you find a dupe that fits let me know! I just want a workable solution.

推荐答案

这可行,是您想要的吗?

This works, is it what you want?

$("td").click(function(e) {
    var chk = $(this).closest("tr").find("input:checkbox").get(0);
    if(e.target != chk)
    {
        chk.checked = !chk.checked;
    }
});

在此处演示

这篇关于jQuery TD onclick设置复选框,不冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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