有点击功能的问题

时间:2012-02-10 15:13:50

标签: javascript jquery html html-table tablerow

HTML

<tr url="domain.com">
<td><input type="checkbox" name="checkbox[]" method="post" value="" class="checkbox" />  </td>
</tr>

JS

$("tr").not('.checkbox').click(function(){
    window.location = $(this).attr("url");

});

想在Clickbox中禁用点击功能,但上面的代码不起作用:即使我点击复选框,它也会重定向。我哪里出错了?

4 个答案:

答案 0 :(得分:2)

$("tr").not('.checkbox')

此代码将匹配没有tr类的所有checkbox元素。如您所见,您的tr没有该类,因此此tr将匹配。

有两种基本方法可以完成您要做的事情。您可以在函数中签入原始单击元素的内容,如果是复选框,则不会重定向。或者,您可以在复选框中添加点击处理程序并调用e.stopPropagation()

$("tr .checkbox").click(function(e){
    e.stopPropagation();
});

答案 1 :(得分:1)

添加一个阻止传播的单独处理程序。

$("tr").click(function(){
    window.location = $(this).attr("url");
})
  .find(".checkbox").click(function(e) { e.stopPropagation(); });

答案 2 :(得分:1)

只需在下面的复选框上停止事件传播,

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

修正了拼写错误。

答案 3 :(得分:1)

单击复选框时停止事件传播。试试这个。

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

将代码更改为

$("tr").click(function(){
    window.location = $(this).attr("url");

});

或者,您可以检查目标并采取相应措施。试试这个。

$("tr").click(function(e){
    if(!$(e.target).is(':checkbox')){
       window.location = $(this).attr("url");
    }
});