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]