Type=file The Change event can only be executed once problem and workaround instance

Source: Internet
Author: User
Recently helped a friend to do a project encountered Type=file change event can only be performed once the problem, degree Niang a bit, found to provide a variety of solutions, so decided to record my thinking direction and the final solution.

At first to help a friend to do a project, the project encountered uploading files, did not think too much, the code knocked Up, the upload done, excitedly, a test, EI, how to do, how can not upload, the programmer of all kinds of grasping nasty ah, the beginning of the Niang find solutions, find to find, calm down, and think of solutions;

Egg <input type= ' file '/> design, regardless of file upload success or not, has been added to the file has been recorded, so will not perform the change event, probably the reason to know, do it, if I can clear the record, the event will not be able to trigger it, try a bit, It's not successful. Continue to find the method, if I replace the <input type= ' file '/> tag, and then rebind the change event what would be, try it out, succeed, and paste the following code:

<form action= "/api/index/browse" enctype= "Multipart/form-data" id= "Form_upload" method= "POST" >
<input id= "fileUpload" type= "file" name= "file" style= "Display:none" >

</form>

$ (' input[id=fileupload] '). Click ();

var count = 0;
$ (' input[id=fileupload] '). Unbind (). Change (function () {
$ (' #form_Upload '). Ajaxsubmit (options);

});

var options = {
Success:function (data) {
$scope. OrderInfo = eval (data);
$scope. $apply (function () {
$scope. OrderInfo = eval (data);

Start to avoid the change of the Type=file control () can only be performed once, replace the control, rebind the event
count++;
$ ("#fileUpload"). ReplaceWith (' <input type= "file" id= "FileUpload" name= "file" style= "Display:none;" title= "' + Count + '/> ');
$ (' #fileUpload '). On (' Change ', function () {

$ (' #form_Upload '). Ajaxsubmit (options);
});
End

$http. Post (URL, params). Success (function (data) {
Do something
}, function () {
Do something

});
});
}
};

Related Article

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.