用js循环添加元素时出现无论点击哪个后面的删除按钮都只能删掉最后一排且只能删除一次

 $(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);

                }
            }
        }
    });
    ![图片说明](https://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就行了

                }

图片说明
图片好像看不了

小妹刚刚注册的没啥币,请帮帮忙咯!

捉急啊!弄了一下午了都找到头绪,几乎要白了我3千青丝了。赶快来个大神救救我吧!