我使用java脚本在html表中创建了一个动态按钮。代码是这样的
$('#dataTables-data').empty();
jQuery.each(searchTO, function(i, item) {
historyrowid += '<tr><td style=" border: 1px solid black;">' + Math.round(item.SNo * 100) / 100+
'</td><td style=" border: 1px solid black;">' +Math.round( item.DistributorId* 100) / 100 +
'</td><td style=" border: 1px solid black;">' +item.DistributorName+
'</td><td style=" border: 1px solid black;">' +item.Bonuspercent +
'</td><td style=" border: 1px solid black;">' +Math.round( item.PrevCumPV * 100) / 100+
'</td><td style=" border: 1px solid black;">' + Math.round(item.exclpv* 100) / 100 +
'</td><td style=" border: 1px solid black;">' + Math.round(item.SelfBV * 100) / 100+
'</td><td style=" border: 1px solid black;">' + Math.round(item.grouppv * 100) / 100+
'</td><td style=" border: 1px solid black;">' + Math.round(item.Totalpv * 100) / 100+
'</td><td style=" border: 1px solid black;">' + Math.round(item.totalBv * 100) / 100+
'</td><td style=" border: 1px solid black;"><button type="button" class="Downline_button" id="'+item.DistributorId+'" >Downline</button></td></tr>';
});
$('#dataTables-example').append(historyrowid);
创建的表是这个
但是当我在此按钮上创建事件时,什么也没有发生。我的按钮上的事件代码是这样的
$(".Downline_button").click(function(){
debugger;
var id = this.id;
});
所以请告诉我如何在动态按钮上创建事件以及如何读取按钮 ID ??
最佳答案
您需要将事件处理程序附加到文档并将事件委托(delegate)给元素。
你可以这样做:
$(document).on('click', '.Downline_button', function(){
debugger;
var id = this.id;
});
这将应用于具有此 id 的所有元素,无论它们是否在 document.ready() 之前创建。
关于javascript - 动态按钮脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027321/