我有以下问题: 我使用ajax查询来检索客户信息,返回的信息填充表格。仅显示姓氏,名字,以及customerID的display:none字段。
我需要这样,当用户在此表中双击一行时,将使用customerID检索有关所选客户的详细信息。
但是:显然jquery不允许对动态生成的行进行事件处理。
Jquery生成行:(有效):
var tblRow ="tr class='2clicks'"+"td style='display:none'"+cust.Code_Client+"/td"+"td"+cust.FirstName+"/td"+"td"+cust.lastName+/td+td+cust.DOB+"/td"+"/tr" ;
$(tblRow).appendTo("#inserthere");
通过课程处理事件:
$(".2clicks").dblclick( function (){
alert("ok");
});
在搜索stackoverflow后尝试使用“on”,但没有工作:
$("#inserthere tr td").on("dblclick", function(event){
alert($(this).parent().children().filter(':first').text())
});
感谢任何帮助,谢谢
Jquery活动
答案 0 :(得分:1)
如果您没有使用最新版本的jquery,那么您可以使用
$(".2clicks").live('click', function(e){
// code here
});
如果您使用的是最新版本,则可以使用
$('#inserthere').on('click', '.2clicks', function(e){
// code here
});
Live已在较新版本中弃用。
答案 1 :(得分:0)
要使用on()
委派事件,您需要在父静态元素上调用它,您确定它将在DOM准备就绪,然后将动态子选择器传递给委托事件。
$('#inserthere').on('dblclick', 'td', function (e) {
// Something here
});