How to use JS to empty the value of the upload control input (type= "file")

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax classic clear code control document file function

var obj= Web page <input type= "file"/>
obj.outerhtml = Obj.outerhtml.replace (/(value=). + "/I," $ ");

More classic approach

You need to clear the value of the <input type= "file" >, but the value of the upload control <input type= "file" > cannot be modified by the Web effect.

Google found a workaround:


Inserting a value in the upload control only clears the form's reset function, but the other values in the form are reset.

Since you can use the reset of the form to empty, there is a way: Create a temporary form, and then the need to empty the upload control to move into it, reset, then move back to the original location, and finally delete the created temporary form. Web page Special effects code:

Web page Special effects code


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&lt;inputs.length; i++)


{   


if (Inputs[i].type = = "File") fs[fs.length] = inputs[i];


}   


  


var tf = document.createelement ("form");


for (var i=0 i&lt;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&lt;fs.length; i++)


{   


var tt = document.getElementById ("__tt__" + i);


Tt.parentnode.insertbefore (FS[I],TT);


tt.parentnode.removechild (TT);


}   


Tf.parentnode.removechild (TF);


}   


}

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;  = 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); } }

This method uses the example:


HTML code
<!doctype HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">   
<html xmlns=" http://www.w3.org/1999/xhtml ">    
<head>   
<title>test</title>   
<script Type= "text/web Effects" >   
<!--introduce the above JS code--></script>   
</head >   
  
<body>   
<form name= "Testform"   Method= "POST" >   
<input type= "file" Name= "testfile"/>   
<input Type= "button" value= "clear" onclick= "upload.clear (' testfile ')"/><br/>   
<input Type= "button" value= "ClearAll" onclick= "Upload.clearform ()"/><br/>   
<input type= " Submit "value=" submit "/><input type=" reset "value=" reset "/>&NBSP;&NBSP;&NBsp;
</form>   
</body>   

</html>

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.