$(function(){
alert("fdsafsd");
var fileNames = $("#fileName").val();
var filePaths = $("#filePath").val();
var fileNameList = new Array();
var filePathList = new Array();
if(fileNames.length !=0 && fileNames.length != "" && filePaths.length !=0 && filePaths.length !=""){
fileNameList = fileNames.split(", ");
filePathList = filePaths.split(", ");
alert(fileNameList[0]+fileNameList[1]+fileNameList[2]);
alert(filePathList.length);
for(i = 0; i < fileNameList.length; i++){
$("#i").val(i+1);
alert($("#i").val());
var td = document.getElementById("more");
var br = document.createElement("br");
var a = document.createElement("a");
var input = document.createElement("input");
var input1 = document.createElement("input");
var input2 = document.createElement("input");
var button = document.createElement("input");
var span=document.createElement("span");
a.id= "downloadFile" + i;
a.innerHTML = ">>附件下載";
a.href = "${baseURL}/" + filePathList[i];
a.download = fileNameList[i];
input.id = "attachmentFile" + i;
input.disabled = "disabled";
input.style = "background: transparent; border: none;";
input.value = fileNameList[i];
input.type = "text";
input1.value = fileNameList[i];
input1.type = "text";
input1.name = "vo.updownFileName";
input1.id = "updownFileName" + i;
input2.value = filePathList[i];
input2.type = "text";
input2.id = "updownFilePath" + i;
input2.name= "vo.updownFilePath";
button.type = "button";
button.value = "刪除";
td.appendChild(br);
td.appendChild(a);
td.appendChild(input);
td.appendChild(input1);
td.appendChild(input2);
td.appendChild(span);
td.appendChild(button);
button.onclick = function()
{
alert("刪除");
td.removeChild(br);
td.removeChild(a);
td.removeChild(input);
td.removeChild(input1);
td.removeChild(input2);
td.removeChild(span);
td.removeChild(button);
}
}
}
});
![圖片說明](http://img.ask.csdn.net/upload/201604/18/1460972877_485212.png)
請大神幫我看看到底是哪裡的原因?
閉包沒做好,導致刪除的是最後的那行的控件,刪除後你再執行刪掉操作dom對象不存在就會報錯了,改下結構,用div擴起容器,然後通過dom關系來刪除div就行了
input2.name = "vo.updownFilePath";
button.type = "button";
button.value = "刪除";
////////////////////////////
var div=document.createElement('div')////////////
div.appendChild(br);
div.appendChild(a);
div.appendChild(input);
div.appendChild(input1);
div.appendChild(input2);
div.appendChild(span);
div.appendChild(button);
td.appendChild(div)////////////
////////////////////////////
button.onclick = function () {
alert("刪除");
td.removeChild(this.parentNode);//this為當前button,parentNode就是button的div容器,td直接刪除這個div就行了
}