2

Im havin problems in selecting my first 2 td of every tr(i need to make them lickable) and my last td of every tr(different links). Can someone help me with the code? I cant seem tofigure it out. The code seems legit but it doesnt work. Here is the js and html:

JS

$(".rand_notif td:lt(2)").click(function(){
    $(".rand_notif").html("asd");
})

HTML (somehow..still js but html)

$.each(data.notif,function(i,x){
    var cant='';

    if(x.cant>0){var cant = x.cant+"x";}
    notificari+="<tr class='spacer_2'></tr><tr class='notificari rand_notif' record='"+x.id+"'><td>"+cant+"</td><td>"+x.nume+"</td><td>Refuz</td></tr>";
});

Actual html

<table cellspacing="0" id="tabel_notificari">
    <tr class="spacer_1"></tr>
    <tr class="table_head notificari">
        <th width="30"></th>
        <th>Notificari</th>
        <th width="86"></th>
    </tr>
</table>

EDIT: Problem solved. The problem was, as explained in the comments, the fact that the elements were first binded then added, therefore the bind didnt exist. Solution:

$("#tabel_notificari").on("click", ".rand_notif td:lt(2)",function(){$(".rand_notif").html("asd");});
4

2 回答 2

2

您正在通过 javascript 创建表格单元格,因此您需要使用不同的方法将点击事件附加到它们。尝试使用附加到表本身的“ON”方法,如下所示。这将适用于 DOM 加载后添加到表中的任何 TD。

$("#tabel_notificari").on("click", ".rand_notif td:lt(2)", function(e) {
   $(this).html("asd");
});
于 2013-04-30T20:21:10.013 回答
1

您的 HTML 中没有 TD 标签。你是说TH吗?

于 2013-04-30T20:14:40.913 回答