Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 关于<;tr>;标记,但不包括细颗粒<;td>;_Jquery - Fatal编程技术网

Jquery 关于<;tr>;标记,但不包括细颗粒<;td>;

Jquery 关于<;tr>;标记,但不包括细颗粒<;td>;,jquery,Jquery,我对事件处理有问题 下面是一个例子: <table> <tr> <td><input type='checkbox' id='xxx'/><td> <td>a1</td> <td>b1</td> </tr> <tr> <td><input type='check

我对事件处理有问题

下面是一个例子:

<table>
    <tr>
        <td><input type='checkbox' id='xxx'/><td>
        <td>a1</td>
        <td>b1</td>
    </tr>
    <tr>
        <td><input type='checkbox' id='xxx'/><td>
        <td>a2</td>
        <td>b2</td>
    </tr>
    <tr>
        <td><input type='checkbox' id='xxx'/><td>
        <td>a3</td>
        <td>b3</td>
    </tr>
    ....
</table>

a1
b1
a2
b2
a3
b3
....
我绑定单击
tr
标记上的事件,然后即使选中了
复选框
,它也被称为单击事件

除了这个我该怎么办

在标记上绑定事件是不好的。因为,td标签的数量不受限制

谢谢你的帮助。

试试看

$('table').on('click', 'tr', function(){
    //your code
})

$('table').on('click', 'input:checkbox', function(e){
    e.stopPropagation();
})

演示:

将类添加到复选框中

<td><input type='checkbox' class="cb" id='xxx'/></td>

在修复了混乱的标签后,我通常会做的是:

$("tr").click(function(e) {
    if(!$(e.target).is("input[type='checkbox']")) {
        alert("No checkbox");
    }
});

您使用的html无效,除非您的代码复制中有输入错误。看起来您的html被弄乱了
没有关闭。您多次使用相同的ID(
xxx
)。@varnie我确信他的真实ID不是“xxx”对不起。我错过了打字^^
$("tr").click(function(e) {
    if(!$(e.target).is("input[type='checkbox']")) {
        alert("No checkbox");
    }
});