我想求教一下,就是如何才能使得js追加的數組數據傳到後台時不與table數組沖突,例如下面table數組傳遞數據位single[0].id,single[0].name,single[1].id,single[1].name,那麼追加的數組就應該從single[2].id,single[2].name開始傳遞,這樣才能使數據不沖突,但是這個如何實現呢?求哪位大神幫忙解答下哈,感謝!!!下面附上代碼:
function addTr(value) { var otable = document.getElementById("table1"); for ( var i = 0, len = value.length; i < len; i++) { var temp = value[i].split("#"); var myTR = otable.insertRow(otable.rows.length); var myTD1 = myTR.insertCell(0); myTD1.innerHTML = "<input type='text' id='mid' readonly='readonly' name='singles["+j+"].id' value="+temp[0]+">"; var myTD2 = myTR.insertCell(1); myTD2.innerHTML = "<input type='text' readonly='readonly' name='singles["+j+"].name' value="+temp[2]+">"; j++;} }頁面table顯示代碼
通過行數控制input的name生成。。不過行數不能刪除。。刪除會導致重復
function addTr(value) {
var otable = document.getElementById("table1");
var j = otable.rows.length;//////////
for (var i = 0, len = value.length; i < len; i++) {
var temp = value[i].split("#");
var myTR = otable.insertRow(otable.rows.length);
var myTD1 = myTR.insertCell(0);
myTD1.innerHTML = "<input type='text' id='mid' readonly='readonly' name='singles[" + j + "].id' value=" + temp[0] + ">";
var myTD2 = myTR.insertCell(1);
myTD2.innerHTML = "<input type='text' readonly='readonly' name='singles[" + j + "].name' value=" + temp[2] + ">";
}
}
你的代碼不是已經用j控制name生成了嗎?一直遞增就好了。。只不過刪除的時候會出現空項,你在服務器端要判斷過