简体   繁体   English

如何使整行以及其中的复选框都可以单击,然后在我在表行内部单击时单击并取消单击?

[英]How to make entire row along with the checkbox in it, click and unclick when I anywhere click inside the table row?

I have a table 我有桌子

<tr>
    <td><input name="service_id[]" value="17" type="checkbox"></td>
    <td class="right">$175.00</td>
    <td>add 0 days to delivery</td>
</tr>

And jQuery 和jQuery

$('#tableSelect tr').click(function() {
    if ($(this).find('td input:checkbox').prop('checked') == true)
        $(this).find('td input:checkbox').prop('checked', false);
    else    
        $(this).find('td input:checkbox').prop('checked', true);
});

My goal is to have the checkbox click ON and OFF as I click on anywhere in the table row . 我的目标是在单击表行中的任何位置时,使复选框分别单击“打开”和“关闭”。

However what I get is I can click anywhere on the table row to click and unclick, but when I click inside the checkbox itself, it does not register my click. 但是我得到的是我可以单击表格行中的任意位置以单击和取消单击,但是当我在复选框本身内单击时,它不会记录我的单击。

How can I make entire row and the checkbox in the row click and unclick when I click anywhere inside the entire table row, including entire the checkbox itself? 当我单击整个表格行内的任何位置(包括整个复选框本身)时,如何使整个行和该行中的复选框单击并取消单击?

Add a click event to the input s and then use stopPropagation to prevent event bubbling. 将click事件添加到input s,然后使用stopPropagation防止事件冒泡。

 $('#tableSelect tr').click(function() { ele = $(this).find('td input:checkbox')[0]; ele.checked = ! ele.checked; }); $('input:checkbox').click(function(e){ e.stopPropagation(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table id="tableSelect"> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> </table> 

You need to register change event also, so bind it like this: 您还需要注册change event ,因此像这样绑定它:

$('#tableSelect tr').on('click change',function() {
    if ($(this).find('td input:checkbox').prop('checked') === true) 
        $(this).find('td input:checkbox').prop('checked', false);
    else    
        $(this).find('td input:checkbox').prop('checked', true);
});

Check the below snippet 检查以下代码段

 $('#tableSelect tr').on('click change', function() { if ($(this).find('td input:checkbox').prop('checked') === true) $(this).find('td input:checkbox').prop('checked', false); else $(this).find('td input:checkbox').prop('checked', true); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="tableSelect"> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> <tr> <td> <input name="service_id[]" value="17" type="checkbox"> </td> <td class="right">$175.00</td> <td>add 0 days to delivery</td> </tr> </table> 

$('#tableSelect tr *').click(function(e) {
  var $cb = $(this).parent('tr').find('input[type=checkbox]');
  var c = $cb.prop('checked');
  if (c === undefined)
    e.stopPropagation();
  else
    $cb.prop('checked', !c);
});

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

相关问题 单击表格行上的任意位置,它将检查其所在的复选框...? - click anywhere on a table row and it will check the checkbox its in…? 在表格行中有一个输入复选框,我可以将click事件添加到行,仍然可以单击输入 - with an input checkbox inside a table row, can i add click event to row and still be able to click input 计数+ /-对于复选框,单击并取消单击 - Count + / - for checkbox click and unclick 单击该行上的任意位置以选中该复选框并突出显示它? - click anywhere on the row to check the checkbox and highlight it? 单击或选择行上的任意位置以选中复选框并突出显示它? - click or select anywhere on the row to check the checkbox and highlight it? 使用Angular如何停止表格行内复选框单击事件的传播()? - with Angular how to stopPropagation() of checkbox click event inside a table row? 单击并取消选中带有计数的复选框 - Click and unclick checkbox with count 如果选中了复选框(单击按钮),如何使表格行突出显示? - How to make a table row highlight if the checkbox is checked (on button click)? 点击该行中的任何位置,如何触发点击该“链接”? - How to trigger a click of that 'link' on click of anywhere in that row? 在表行中的更改复选框上运行时如何停止重叠单击? - How to stop overlapping click when run on change checkbox in table row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM