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