The example in this article describes how to set the number of files when uploading a form using JavaScript. Share it with everyone for your reference. The details are as follows:
This is a more practical function. When using JavaScript to set up a form to upload files, the upload form can be generated as needed. You need to generate several forms. This function is available in the add attachment function of NetEase mailbox and Sina mailbox. However, this one does not have the function of deleting the form. If you enter too much, you will have to start over
The operation effect is as shown below:
The specific code is as follows:
<title>JavaScript设置表单上传时的文件个数</title> <input type="button" name="button" value="添加附件" onclick="addInput()"> <input type="button" name="button" value="删除附件" onclick="deleteInput()"> <span id="upload"></span> <script type="text/javascript"> var attachname = "attach"; var i=1; function addInput(){ if(i>0){ var attach = attachname + i ; if(createInput(attach)) i=i+1; } } function deleteInput(){ if(i>1){ i=i-1; if(!removeInput()) i=i+1; } } function createInput(nm){ var aElement=document.createElement("input"); aElement.name=nm; aElement.id=nm; aElement.type="file"; aElement.size="50"; //aElement.value="thanks"; //aElement.onclick=Function("asdf()"); if(document.getElementById("upload").appendChild(aElement) == null) return false; return true; } function removeInput(nm){ var aElement = document.getElementById("upload"); if(aElement.removeChild(aElement.lastChild) == null) return false; return true; } </script>
I hope this article will be helpful to everyone’s JavaScript programming design.