The examples in this article describe the way JavaScript implements empty (reset) file type INPUT element values. Share to everyone for your reference, specific as follows:
Because of security restrictions, scripts cannot set their value values arbitrarily, so they cannot be set to reset with attributes as other form input fields do.
Resetting the value of a file field can be summed up in 3 main ways.
This article analyzes the browser compatibility and advantages and disadvantages of these three methods, and gives a more perfect combination of code and demo.
Three ways to reset a file field:
1. Set the Value property to null.
For IE11 above and other newer non IE modern browser chrome/firefox/opera ... Effective.
2. Clone or create a new file INPUT element to replace it.
Use createelement or CloneNode clones or create a new element to replace it for all browsers. The disadvantage is also obvious, that is, the replacement will lose the previously bound event listener and lose some of the custom expando properties. Without this problem can be used, not generic, I do not recommend the use of this method.
3. Call the Reset () method of the form form element.
The reset () method of the form element resets all the input elements within the form, which is not what we expect, so you can work around creating a new form object, putting the file INPUT element in and then resetting it, and then removing the file INPUT element and putting it back in place. This will not show the drawbacks of Method 2.
Combined with Method 1 and Method 3, all browsers can be compatible.
The JavaScript function code is as follows:
function Clearinputfile (f) {
if (f.value) {
try{
f.value = ';//for IE11, Latest Chrome/firefox/opera ...
}catch (Err) {
}
if (f.value) {//for IE5 ~ IE10
var form = document.createelement (' form '), ref = f.nextsibling, p = f.parentnode;
Form.appendchild (f);
Form.reset ();
P.insertbefore (F,REF);}}
More readers interested in JavaScript-related content can view the site topics: "JavaScript Search Algorithm Skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary", " A summary of JSON manipulation techniques in JavaScript, a summary of JavaScript switching effects and techniques, a summary of JavaScript animation effects and techniques, JavaScript errors and debugging tips, and a summary of JavaScript mathematical operational usage
I hope this article will help you with JavaScript programming.