Javascript 无法从以编程方式生成的表中触发onclick事件
我有一个以编程方式添加的表,其中包含一个onclick事件,该事件使用我要编辑的行的id调用特定的javascript函数。然而,它并没有被称为。我尝试过通过添加一个类来改变按钮,并尝试在那里获得一些东西,但它不起作用。这过去是有效的,我不知道为什么或者如何修复它Javascript 无法从以编程方式生成的表中触发onclick事件,javascript,jquery,html,Javascript,Jquery,Html,我有一个以编程方式添加的表,其中包含一个onclick事件,该事件使用我要编辑的行的id调用特定的javascript函数。然而,它并没有被称为。我尝试过通过添加一个类来改变按钮,并尝试在那里获得一些东西,但它不起作用。这过去是有效的,我不知道为什么或者如何修复它 function buildTable() { $.ajax({ url: "getTable.php", dataType: "json", success: function(result) {
function buildTable() {
$.ajax({
url: "getTable.php",
dataType: "json",
success: function(result) {
if (result != null) {
$('#measurements tbody tr').remove();
result.forEach(function (measurement) {
var message = "<tr><td>" + dateFromtimestamp(measurement.weigh_date) + "</td>";;
message += "<td>" + measurement.weight + "</td>";
message += "<td><input type='hidden' value='" + measurement.id + "'>" + measurement.waist + "</td>";
message += "<td>" + measurement.upperBp + "</td>";
message += "<td>" + measurement.lowerBp + "</td>";
message += "<td>" + measurement.comment + "</td>";
message += "<td><button type='button' class='btn btn-outline-warning' onlick='editRow(" + measurement.id + ")'>";
message += "<span class='glyphicon glyphicon-pencil'></span></button> ";
message += "<button type='button' class='btn btn-outline-danger' onlick='removeRow(" + measurement.id + ")'>";
message += "<span class='glyphicon glyphicon-remove'></span></button></td></tr>";
$('#measurements').append(message);
});
}
}
});
我尝试通过添加一个类来更改按钮:
并尝试使用$('.editRow')捕获事件。在(“单击”上,function(){}事件具有相同的结果。尝试此方法
$(document).on("click","tr",function() {
console.log(this,"click");
});
多亏了kmgt。如果它是一条蛇,它会咬我。必须使用“onclick”而不是“onlick”。你有一个打字错误
onlick
而不是onclick
也可以考虑使用被动事件侦听器:
$(document).on("click", <target>,<your function>});
$(文档).on(“单击”,});
其中,
是jquery选择器按钮
和
您的回调
这将提高可读性,并有助于保持HTML和JS之间的分离。同时也适用于动态插入的目标。首先,我同意@dmitri-g,您应该使用它来处理在呈现DOM后添加的元素上的任何事件。(动态添加)。之后,您应该尝试调试/打印以控制台所需的任何信息,以确保将正确的值传递到函数中。我准备了示例,将演示此方法:
const结果=[{
称重日期:“2019年1月1日”,
重量:200磅,
id:1,
腰围:“腰围”,
upperBp:“upperBp”,
lowerBp:“lowerBp”,
评论:“评论”
},
{
称重日期:“2015年1月1日”,
重量:“100磅”,
id:2,
腰围:“腰围”,
upperBp:“upperBp”,
lowerBp:“lowerBp”,
评论:“评论”
}
];
函数buildTable(){
如果(结果!=null){
$('#测量值tbody tr')。删除();
结果。forEach(功能(测量){
var message=”“+measurement.weight_date+”;
消息+=“”+measurement.weight+“”;
消息+=“”+measurement.weight+“”;
消息+=“”+measurement.upperBp+“”;
消息+=“”+measurement.lowerBp+“”;
消息+=“”+measurement.comment+“”;
消息+=“编辑”;
消息+=“删除”;
$('#度量')。追加(消息);
});
}
}
$(“#测量”)。在('click','btn edit',function()上{
const id=$(this.attr('id');
console.log('要编辑的ID:'+ID);
//你的东西在这里
});
$('#measurements')。在('click','btn delete',function()上{
const id=$(this.attr('id');
console.log('要删除的ID:'+ID);
//你的东西在这里
});
buildTable();
过去的测量
日期
重量
腰围
收缩期(上)
舒张压(较低)
评论
行动
我喜欢舔我的屏幕让事情正常工作。你有一个打字“OnLIKE=”EddRe},如果<代码>测量。ID/COD>不仅仅是数字的,这将不起作用,因为你在传递它时没有引用它的值。请考虑添加至少几个关于你的代码的单词,你的回答可能比简单的ADDI复杂得多。在构建表时取消“onclick”事件。我确实使用控制台来确保传递了正确的值,因此我要问的是为什么事件没有触发,而不是为什么它使用错误的数据触发。
$(document).on("click","tr",function() {
console.log(this,"click");
});
$(document).on("click", <target>,<your function>});