简体   繁体   English

取消复选框选择时触发点击事件

[英]Cancel triggering of click event on checkbox selection

I have this piece of HTML: 我有这段HT​​ML:

<tr class="acord">
 <td colspan="4"><input class="marcatodos" type="checkbox" value="1"/>Clientes</td>
</tr>
<tr>
   <td>
      <label>
      <input name="vCliente" class="marcar" type="checkbox" checked="checked" value="1" />
      <span class="lbl"> Visualizar Cliente</span>
      </label>
   </td>
   <td>
      <label>
      <input name="aCliente" class="marcar" type="checkbox" value="1" />
      <span class="lbl"> Adicionar Cliente</span>
      </label>
   </td>
   <td>
      <label>
      <input name="eCliente" class="marcar" type="checkbox" value="1" />
      <span class="lbl"> Editar Cliente</span>
      </label>
   </td>
   <td>
      <label>
      <input name="dCliente" class="marcar" type="checkbox" value="1" />
      <span class="lbl"> Excluir Cliente</span>
      </label>
   </td>
</tr>

and this jQuery script: 和这个jQuery脚本:

$(".acord").click(function() {
   $(this).next("tr").slideToggle(1);
});

That's what the HTML generates: 这就是HTML生成的内容:

在此处输入图片说明

The jQuery hides the row below the one with a title (class accord ) jQuery将带有标题(类accord )的行隐藏在下面

My problem is that when I click on the checkbox, the jQuery triggers it anyway. 我的问题是,当我单击复选框时,jQuery仍会触发它。

How can I make it so the function only runs when I click outside the checkbox? 我如何才能使该功能仅在单击复选框外部时运行?

Basically you need to stop the event from bubbling up to the tr element 基本上,您需要阻止事件冒泡至tr元素

$('.marcatodos').click(function(e){
  e.stopPropagation();
});

Here is a fiddle to show the same. 是一个显示相同内容的小提琴。

You can check whether a particular element triggered the click event with the event 's target property. 您可以检查特定元素是否通过eventtarget属性触发了click事件。

$(".acord").click(function(e) {            // Make your event handler accept an event parameter
    if (e.target.tagName === 'INPUT') {    // Any check against the element
        return;
    } 

    $(this).next("tr").slideToggle(1);
});

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

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