Javascript code _ javascript tips for clearing the input (type = "file") value of the upload Control

Source: Internet
Author: User
In a small function recently, you need to clear the value of & lt; inputtype & quot; file & quot; & gt;, but upload the Control & lt; inputtype & quot; file & quot; the value of & gt; cannot be modified using JavaScript. A small feature recently developed, which needs to be cleared But the upload Control The value cannot be modified using JavaScript.

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:

The 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; I F (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
 
  

The 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; I F (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
   
    


Example:
Html code

The Code is as follows:

   Test 
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.