Dynamically add file field forms using JavaScript

Source: Internet
Author: User

Js Code:
Copy codeThe Code is as follows:
<Script language = "javascript">
// Global variable, which represents the number of file domains, and uses this variable to distinguish the name attribute of the file domain
Var file_count = 0;
// Add a file domain
Function additem (id ){
If (file_count> 9 ){
Alert ("up to u22810 u25991 u22495 ");
Return;
}
// Define the row variable, cell variable, and cell content variable, str.
Var row, cell, str;
// Insert a row into the table with the specified id
Row = eval ("document. all [" + '"' + id + '"' + "]"). insertRow ();
If (row! = Null ){
// Set the row background color
Row. bgColor = "white ";
// Insert a cell into the row
Cell = row. insertCell ();
// Set the str value, including a file domain and a delete button
Str = '<input onselectstart = "return false" class = "tf" onpaste = "return false" type = "file" name = "file [' + file_count + ']" style = "width: 500px "onkeydown =" return false; "/> ';
Str + = "<input type =" + '"' +" button "+ '"' + "value =" + '"' +" delete "+ '"' +" onclick = 'deleteitem (this, "+ '"' + "tb" + '"' +"); '> ";
// Increase the number of file Fields
File_count ++;
// Set innerHTML of a cell to str
Cell. innerHTML = str;
}
}
// Delete a file domain
Function deleteitem (obj, id ){
Var rowNum, curRow;
CurRow = obj. parentNode. parentNode;
RowNum = eval ("document. all." + id). rows. length-1;
Eval ("document. all [" + '"' + id + '"' + "]"). deleteRow (curRow. rowIndex );
File_count --;
}
</Script>

Html code:
Copy codeThe Code is as follows:
<Input type = button value = "add" onclick = 'additem ("tb") '/> <br/>
<Table cellspacing = "0" id = "tb" style = "width: 400px">
</Table>

Related Article

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.