JS Dynamic Add File Control

Source: Internet
Author: User

<script language= "javascript" type= "Text/ecmascript" >
//======================
Function: Input file control in the form
Parameters: ParentID---The ID of the parent element to insert the input file control
inputID----The ID of the input file control
//======================
function Createinput (Parentid,inputfileid) {
var parent=$ (ParentID);//Get parent element

var div=document.createelement ("div");//Create a div container to contain the input file
var x=parseint (Math.random () * (80-1)) +1;
var divname=inputfileid+x.tostring ();//The name of the random div container
Div.name=divname;
Div.id=divname;

var aelement=document.createelement ("input"); Create input
Aelement.name=inputfileid;
Aelement.id=inputfileid;
aelement.type= "file";//set type to File

var delbtn=document.createelement ("input");//Create a button to delete the input file
Delbtn.type= "button";
delbtn.value= "Delete";
Delbtn.onclick=function () {removeinput (parentid,divname)};//set the OnClick method for the button

Div.appendchild (aelement);//Add input file to div container
Div.appendchild (DELBTN);//Add the Delete button to the DIV container
Parent.appendchild (div);//Add div container to parent element
}
//============================
function: Delete a div container containing input file
Parameter: ParentID---The parent element ID of the input file control
Deldivid----A div container ID that contains input file
//============================
function Removeinput (parentid,deldivid) {
var parent=$ (ParentID);
Parent.removechild ($ (deldivid));
}
Get elements in a document by element ID
function $ (v) {return document.getElementById (v);}
</script>
<body>
<div align= "left" id= "Div_pic" style= "border:1px solid #CCCCCC" >
<input name= "Picfile" type= "file" id= "Showpicfile" >
</div>
<input type= "button" onclick= "Createinput (' div_pic ', ' picfile ')" name= "button" id= "button" value= "+ continue to add picture" >
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.