我有以下代碼:如何在Jquery中動態添加onclick?
obj2 = JSON.parse(ajax.responseText);
for (var i = 0; i <= obj2.length - 1; i++) {
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
var cell7 = row.insertCell(6);
var cell8 = row.insertCell(7);
cell1.innerHTML = obj2[i].qid;
cell2.innerHTML = obj2[i].question;
cell3.innerHTML = obj2[i].answer1;
cell4.innerHTML = obj2[i].answer2;
cell5.innerHTML = obj2[i].answer3;
cell6.innerHTML = obj2[i].answer4;
$(function(){
var btn = document.createElement("BUTTON");
var t = document.createTextNode("Delete "+cell1.innerHTML); //this works well
btn.appendChild(t);
btn.className="menu_buttons";
$(btn).on('click' ,function(){
delete_(cell1.innerHTML);//this assigns the value of the last iteration to each button
});
cell8.appendChild(btn);
});
}
我遍歷JSON數組和我想要分配的相同的功能(使用不同的參數,根據從陣列的ID),以每個按鈕,並且如我在評論中提到,它用相同的參數覆蓋每個按鈕。 有什麼問題?
哪裏是細胞.. – 2015-02-11 10:52:59
我張貼的全循環 – dexter 2015-02-11 10:57:20