无法将单击添加到仅一个表行

时间:2014-11-20 20:04:43

标签: javascript jquery click add

当我执行以下命令将click函数添加到特定行末尾的单元格时,jQuery将click函数添加到所有行。我在这里想念的是什么?

og_rownum恰好等于数字3而不是字符串,并且表中确实存在nth-child(8)列。

$("#game_lister_table tr:eq("+og_rownum+"), :nth-child(8)").click(function(e){  
  alert('here');
  e.stopPropagation();
});

提前致谢....

3 个答案:

答案 0 :(得分:0)

您似乎只想在桌面上的一行添加一个侦听器。如果是这种情况,只需将id =“yourID”添加到您想要操作的标记中。

然后在jquery中,用

选择它
$('#yourID').click(function(e){
//code here
});

如果你不知道哪一行需要操作,并想以编程方式选择它,那就试试吧。

$("#game_lister_table tr").click(function(e){
    $(this)...//selects the row that was clicked
})

答案 1 :(得分:0)

在像你这样的HTML表中选择特定的Cell / Cells时,我习惯了这段代码

    $("#game_lister_table tr:eq("+og_rownum+"), :nth-child(8)").click(function(e){  
  alert('here');
  e.stopPropagation();
});

但我发现有一种简单的方法是每次创建行或单元格时为单元格创建动态ID,您可以轻松地使用JQuery Selector(行,单元格等等)

var table = document.getElementById("myTable");
var rowIndex =table.rows.length; 
var row = table.insertRow();
var cell1 = row.insertCell(0);
cell1.id="whatEver";
cell1.innerHTML = "<input type="text" id="txt'+rowIndex+'" />";

然后你的行动来了Next

 $('#ID').on('click',function(e){});

答案 2 :(得分:0)

由于删除了最接近的答案,我将添加此内容。

使用"#game_lister_table tr:eq("+og_rownum+") td:nth-child(8)"选择您描述的元素。