JS empties the value of Input[type=file]

Source: Internet
Author: User

JS cannot operate Input[type=file]
But you can delete the DOM element of this input, add another one, or replace

$ ("#UploadFile"). ReplaceWith (' <input id= "uploadfile" type= "file"/> ');

如果想在选同一文件上传时也会触发change事件

Need to be executed in the Change event callback for input

$ ("#UploadFile"). ReplaceWith (' <input id= "uploadfile" type= "file"/> ');

Replace can

But a change () event needs to be bound at once;

Changebind:function () {

$ ("#UploadFile"). Change (function () {
var FilePath = $ (this). Val ();
if (Filepath.indexof ("docx")! =-1 | | filepath.indexof ("xlsx")! =-1 | | filepath.indexof ("pptx")! =-1) {
$ (". Showfilename"). Val (FilePath);
UploadFile ();
} else {
$ (". Showfilename"). Val ("");
Alert ("You did not upload the file, or you uploaded a file of the wrong type!") ");
return False
}
});
},


Uploadfile:function (file) {
var FilePath = $ ("#UploadFile"). Val ();
The regular expression gets the file name without the suffix.
var name = Filepath.replace (/^.+?\\ ([^\\]+?) (\. [^\.\\]*?)? $/gi, "$");

Regular expression gets the suffix
var suffix = filepath.replace (/.+\./, "");

var fileName = name + "." + suffix;
if ($.trim (filePath) = = "") {
Alert ("Please select the file you want to upload!") ");
Return
}
Common.ajax ({Default: "FileUpload", Filepath:filepath, Filename:filename}, function () {

}, function (data) {

}, function () {
$ ("#UploadFile"). ReplaceWith (' <input id= "uploadfile" type= "file"/> '); DOM element substitution
Changebind (); Change Event Binding
});
},

JS empties the value of Input[type=file]

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.