Home >Web Front-end >JS Tutorial >js gets and clears the value of input type='file' (sample code)_javascript skills

js gets and clears the value of input type='file' (sample code)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:07:301495browse

The upload control () is used to browse and upload files on the client. The path selected by the user can be obtained by the value attribute, but the value attribute is read-only and cannot be accessed through javascript. Assignment, which makes it impossible to clear it through the value="" statement. It is easy to understand why it is read-only. If the value can be assigned at will, then as long as the user opens your web page, you can upload the files on his computer as he likes.

js gets the value of

Copy code The code is as follows :



MyHtml.html






Two methods to clear the value of the upload control ()

Method 1:

Copy code The code is as follows:






Method 2:
Copy code The code is as follows:

function clearFileInput(file){
var form=document.createElement('form');
document.body.appendChild(form);

//Remember the position of file in the old form
var pos=file.nextSibling;
form.appendChild(file);
form.reset();
pos.parentNode .insertBefore(file,pos);
document.body.removeChild(form);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn