Page 1/2 of the Code for clearing the input (type = & quot; file & quot;) value of the upload control in JS

Source: Internet
Author: User

Google found this solution:
When a value is inserted in the upload control, it can only be cleared through the form reset function, but other values in the form are also reset.
Now that you can use the form reset to clear, there is a way: create a temporary form, and then move the upload control to be cleared into it. After the reset, move back to the original location, finally, delete the created temporary form. Js Code:
Copy codeThe Code is as follows:
Var Upload = {
Clear: function (id ){
Var up = (typeof id = "string ")? Document. getElementById (id): id;
If (typeof up! = "Object") return null;
Var tt = document. createElement ("span ");
Tt. id = "_ tt __";
Up. parentNode. insertBefore (tt, up );
Var tf = document. createElement ("form ");
Tf. appendChild (up );
Document. getElementsByTagName ("body") [0]. appendChild (tf );
Tf. reset ();
Tt. parentNode. insertBefore (up, tt );
Tt. parentNode. removeChild (tt );
Tt = null;
Tf. parentNode. removeChild (tf );
},
ClearForm: function (){
Var inputs, frm;
If (arguments. length = 0)
{
Inputs = document. getElementsByTagName ("input ");
} Else {
Frm = (typeof arguments [0] = "string ")? Document. getElementById (arguments [0]): arguments [0];
If (typeof frm! = "Object") return null;
Inputs = frm. getElementsByTagName ("input ");
}
Var fs = [];
For (var I = 0; I <inputs. length; I ++)
{
If (inputs [I]. type = "file") fs [fs. length] = inputs [I];
}
Var tf = document. createElement ("form ");
For (var I = 0; I <fs. length; I ++)
{
Var tt = document. createElement ("span ");
Tt. id = "_ tt _" + I;
Fs [I]. parentNode. insertBefore (tt, fs [I]);
Tf. appendChild (fs [I]);
}
Document. getElementsByTagName ("body") [0]. appendChild (tf );
Tf. reset ();
For (var I = 0; I <fs. length; I ++)
{
Var tt = document. getElementById ("_ tt _" + I );
Tt. parentNode. insertBefore (fs [I], tt );
Tt. parentNode. removeChild (tt );
}
Tf. parentNode. removeChild (tf );
}
}
View plaincopy to clipboardprint?
Var Upload = {
Clear: function (id ){
Var up = (typeof id = "string ")? Document. getElementById (id): id;
If (typeof up! = "Object") return null;
Var tt = document. createElement ("span ");
Tt. id = "_ tt __";
Up. parentNode. insertBefore (tt, up );
Var tf = document. createElement ("form ");
Tf. appendChild (up );
Document. getElementsByTagName ("body") [0]. appendChild (tf );
Tf. reset ();
Tt. parentNode. insertBefore (up, tt );
Tt. parentNode. removeChild (tt );
Tt = null;
Tf. parentNode. removeChild (tf );
},
ClearForm: function (){
Var inputs, frm;
If (arguments. length = 0)
{
Inputs = document. getElementsByTagName ("input ");
} Else {
Frm = (typeof arguments [0] = "string ")? Document. getElementById (arguments [0]): arguments [0];
If (typeof frm! = "Object") return null;
Inputs = frm. getElementsByTagName ("input ");
}
Var fs = [];
For (var I = 0; I <inputs. length; I ++)
{
If (inputs [I]. type = "file") fs [fs. length] = inputs [I];
}
Var tf = document. createElement ("form ");
For (var I = 0; I <fs. length; I ++)
{
Var tt = document. createElement ("span ");
Tt. id = "_ tt _" + I;
Fs [I]. parentNode. insertBefore (tt, fs [I]);
Tf. appendChild (fs [I]);
}
Document. getElementsByTagName ("body") [0]. appendChild (tf );
Tf. reset ();
For (var I = 0; I <fs. length; I ++)
{
Var tt = document. getElementById ("_ tt _" + I );
Tt. parentNode. insertBefore (fs [I], tt );
Tt. parentNode. removeChild (tt );
}
Tf. parentNode. removeChild (tf );
}
}

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.