我正在尝试创建使用 ajax 创建行的表。问题是,当我根据类名分配“点击”侦听器时,它会被多次调用
我的代码是
function fn_getAlertRules(parentRowId) {
$.ajax({
type: "GET",
url: anyURL,
contentType: "application/json",
dataType: "json"
}).done(function(data) {
// create row to add to parent table's row
var s_rulesHtmlString = '<tr><td colspan="3" class="rules-parent-tr"><table><tbody>';
$.each(data, function(i, name) {
s_rulesHtmlString += '<tr class="rule-tr">';
s_rulesHtmlString += '<td class="eventid-td">Rule ID:'+ name.RuleId+'<span>' + name.OccuredEventId + '</span></td>';
s_rulesHtmlString += '<td>' + name.Name + '</td><td>' + name.RuleDate + '</td>';
s_rulesHtmlString += '</tr>';
});
s_rulesHtmlString += '</tbody></table></td></tr>';
// add row below parent tr
$(parentRowId).parent().after(s_rulesHtmlString);
$(".rule-tr").on("click", "td", function(event) {
// this code blocks get executed multiple times
});
});
}
谁能告诉我,为什么它被多次调用?