Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:element.innerHTML+;=&引用&引用;重置文件输入';s_Javascript_Html - Fatal编程技术网

Javascript:element.innerHTML+;=&引用&引用;重置文件输入';s

Javascript:element.innerHTML+;=&引用&引用;重置文件输入';s,javascript,html,Javascript,Html,我已经开始了一个新的项目,在我开始工作的第一件事上,我真的很痛苦。我正在制作一个表单,一个人可以上传许多“附件”。问题是当这个人按下“添加附件”时,所有附件都会被重置 代码: 。。。 函数addAttachment(){ var list=document.getElementById(“附件”); var指数=1; 对于(;索引

我已经开始了一个新的项目,在我开始工作的第一件事上,我真的很痛苦。我正在制作一个表单,一个人可以上传许多“附件”。问题是当这个人按下“添加附件”时,所有附件都会被重置

代码:

。。。
函数addAttachment(){
var list=document.getElementById(“附件”);
var指数=1;
对于(;索引<6;索引++){
if(document.getElementById(“附件”+索引)==null){
打破
}else if(document.getElementById(“附件”+索引)。值==“”){
指数=-1;
打破
}
}
如果(索引==5){
警报('最多只能有5个附件!');
}else if(索引==-1){
警报('您的一个附件中没有文件!请先将您的文件放在那里!');
}否则{
list.innerHTML+='
'; } } ...
  • 附件:()
  • ...
    我想我的问题是。。。有没有更好的办法


    谢谢你的帮助

    应该使用DOM和appendChild,而不是设置innerHTML

        function addAttachment() {
            var list = document.getElementById("attachments");
    
            var index = 1;
    
            for(; index < 6;index++) {
                if(document.getElementById("attachment" + index) == null) {
                    break;
                } else if(document.getElementById("attachment" + index).value == "") {
                    index = -1;
                    break;
                }
            }
            if(index == 5) {
                alert ('You can only have a maximum of 5 attachments!');
            } else if(index == -1) {
                alert ('One of your attachments do not have a file in it! Put your file in there first!'); 
            } else {
                var br = document.createElement("br");
                var newAttachment = document.createElement("input");
                newAttachment.size = 1;
                newAttachment.type = "file";
                newAttachment.id = "attachment"+index;
                newAttachment.name = newAttachment.id;
    
                list.appendChild(br);
                list.appendChild(newAttachment);
            }
        }
    
    函数addAttachment(){
    var list=document.getElementById(“附件”);
    var指数=1;
    对于(;索引<6;索引++){
    if(document.getElementById(“附件”+索引)==null){
    打破
    }else if(document.getElementById(“附件”+索引)。值==“”){
    指数=-1;
    打破
    }
    }
    如果(索引==5){
    警报('最多只能有5个附件!');
    }else if(索引==-1){
    警报('您的一个附件中没有文件!请先将您的文件放在那里!');
    }否则{
    var br=document.createElement(“br”);
    var newAttachment=document.createElement(“输入”);
    newAttachment.size=1;
    newAttachment.type=“文件”;
    newAttachment.id=“附件”+索引;
    newAttachment.name=newAttachment.id;
    列表。追加子项(br);
    列表.appendChild(新附件);
    }
    }
    
    您应该使用DOM和appendChild,而不是设置innerHTML

        function addAttachment() {
            var list = document.getElementById("attachments");
    
            var index = 1;
    
            for(; index < 6;index++) {
                if(document.getElementById("attachment" + index) == null) {
                    break;
                } else if(document.getElementById("attachment" + index).value == "") {
                    index = -1;
                    break;
                }
            }
            if(index == 5) {
                alert ('You can only have a maximum of 5 attachments!');
            } else if(index == -1) {
                alert ('One of your attachments do not have a file in it! Put your file in there first!'); 
            } else {
                var br = document.createElement("br");
                var newAttachment = document.createElement("input");
                newAttachment.size = 1;
                newAttachment.type = "file";
                newAttachment.id = "attachment"+index;
                newAttachment.name = newAttachment.id;
    
                list.appendChild(br);
                list.appendChild(newAttachment);
            }
        }
    
    函数addAttachment(){
    var list=document.getElementById(“附件”);
    var指数=1;
    对于(;索引<6;索引++){
    if(document.getElementById(“附件”+索引)==null){
    打破
    }else if(document.getElementById(“附件”+索引)。值==“”){
    指数=-1;
    打破
    }
    }
    如果(索引==5){
    警报('最多只能有5个附件!');
    }else if(索引==-1){
    警报('您的一个附件中没有文件!请先将您的文件放在那里!');
    }否则{
    var br=document.createElement(“br”);
    var newAttachment=document.createElement(“输入”);
    newAttachment.size=1;
    newAttachment.type=“文件”;
    newAttachment.id=“附件”+索引;
    newAttachment.name=newAttachment.id;
    列表。追加子项(br);
    列表.appendChild(新附件);
    }
    }