huaitianqi2016 2016-04-18 09:50 采纳率: 100%
浏览 2515
已采纳

用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)
    请大神帮我看看到底是哪里的原因?
  • 写回答

3条回答

  • Go 旅城通票 2016-04-19 01:56
    关注

    闭包没做好,导致删除的是最后的那行的控件,删除后你再执行删掉操作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就行了
    
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧