简体   繁体   English

单击表行时选中和取消选中复选框

[英]Check and Uncheck Checkbox when Table Row is Clicked

Right now I have my script setup so that when each checkbox inside a table row is checked it performs the functions required (addClass and some others). 现在,我已经设置了脚本,以便选中表行中的每个复选框时,它会执行所需的功能(addClass和其他一些功能)。

I would also like the checkboxs to check/uncheck and perform the same functions when the individual table row is clicked. 我还希望单击单个表格行时,复选框可以选中/取消选中并执行相同的功能。

Here is my code for the checkbox functions: 这是我的复选框功能代码:

$('input[type="checkbox"]').bind('click',function(e) {
        var $this = $(this);
        if($this.is(':checked')) { 
               num += 1;
               $('#delete_btn').fadeIn('fast');
               $this.parents('tr').addClass('selected'); 
               select_arr.unshift(this.id);
        } else { 
               num -= 1;
               if(num  <= 0) { 
                $('#delete_btn').fadeOut('fast'); 
               }
           $this.parents('tr').removeClass('selected'); 
               select_arr.shift(this.id);
        }
    });

What would be the best way to achieve the same result that this code does by just clicking the table row itself rather than the checkbox, but still allowing the checkboxes to function the same. 通过单击表行本身而不是复选框,可以达到与代码相同的结果的最佳方法是什么,但仍然允许复选框发挥相同的功能。

Here is the table: 表格如下: 在此处输入图片说明

Thanks in Advance. 提前致谢。

$("tr").click(function(){
    $(this).child("input:checkbox").eq(0).click();
});

You may want to add a class to the tr tags so that they are discernable from other tr tags on page (ex: <tr class="ticketTR">.. and just add the click function to those::: 您可能想要向tr标记添加一个类,以便与页面上的其他tr标记(例如:<tr class =“ ticketTR”> ..)区分开来,只需将click函数添加到这些标记中::::

$("tr.ticketTR").click(function(){
    $(this).child("input:checkbox").eq(0).click();
});

I ended up finding a great resource for this exact problem. 我最终找到了解决此确切问题的重要资源。

Check it out: 看看这个:

Quick Tip: Click Table Row to Trigger a Checkbox Click 快速提示:单击表行以触发复选框单击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM