var tbody = $("#queryModalTable tbody");
list_of_tasks.forEach(function (t) {
tbody.append(
$(document.createElement("tr")).append(
$(document.createElement("td")).text(t.task_id),
$(document.createElement("td")).text(t.task_status),
$(document.createElement("td")).text(t.task_operator),
$(document.createElement("td")).text(t.number_of_hits),
$(document.createElement("td")).text(t.finished_hits),
$(document.createElement("td")).append(
$(document.createElement("button"))
.addClass("btn btn-primary")
.attr("type", "button")
.attr("onclick","showTaskResult(t)")
.attr("data-toggle", "collapse")
.attr("data-target", "#collapseExample")
.attr("aria-expanded", "false")
.attr("aria-controls", "collapseExample")
.text("show task results")
)
)
);
});
只是想知道为什么无法触发showTaskResult(t)函数,onclick与“data-toggle”,“collapse”之间是否存在冲突?
答案 0 :(得分:0)
由于您将onclick作为字符串属性添加,因此不会在当前上下文中对其进行评估,t
变量在触发时不在范围内。
解决方案是分配点击处理程序,如下所示:
$(document.createElement("button"))
.addClass("btn btn-primary")
...
...
...
.click(function(){
showTaskResult(t);
})