我需要将行参数传递给我的onclick函数。如何将参数发送到JavaScript中的onclick函数
这是我的代码:
function renderHostTableRowJob (dataTable) {
for (var i in dataTable) {
var notification = dataTable[i];
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.innerText = notification["Name"];
row.appendChild(cell);
var cell = document.createElement("td");
cell.innerText = notification["State"];
row.appendChild(cell);
var cell = document.createElement("td");
cell.innerText = (notification["NotificationReceived"] === true) ? "Received" : "Missing";
row.appendChild(cell);
row.onclick = function() {alert(notification["Name"]);};
$("#" + notification["Client"] + "_TableJobDetails > #" + notification["Client"] + notification["HostFormated"] + "_TableBodyJobDetails")[0].appendChild(row);
}
}
目前我所有的row.onclick = function() {alert(notification["Name"]);};
正在返回在我的循环最后一次迭代值...
问题:我怎样才能把我的价值观到每次迭代的点击事件?
感谢
嗨@icktoofay感谢您的快速反应,你能告诉如何捕捉通知作为paremeter一个例子。我知道我正在使用jQuery,但我也在学习JavaScript。非常感谢 –
@Manuel:我做到了;使用'$ .each'来做到这一点。如果你想在没有'$ .each'的情况下做到这一点,你可以用一个IIFE手动完成,例如'!function(someVariable){/ * ... * /}(someVariable)'。 – icktoofay