Process the upload of images with form+input[type= "file" on the same page cannot empty the problem

Source: Internet
Author: User



This afternoon to help colleagues changed such a bug:


when uploading multiple images on a single page, the value of input cannot be a problem, resulting in the same effect as the first picture after each selection:





Baidu took the next train of thought: first to take input, and then put into a temporary form inside empty, and finally delete the form.



The code is as follows:


The first is to generate the binding code for the picture selection and Display section:


 var img_tmp = ' <tr><td><input type= ' text "name=" code "value=" "style=" width:90%; Display:none; " ><input type= "text" name= "title[" "style=" width:90%; "/></td><td><input type=" text "Name=" Sort[] "style=" width:90%; "/></td><td><input type=" text "Name=" num[] "style=" width:90%; " /></td> ' + ' <td><div class=" File_box "> ' + ' <input Type= "button" class= "btn" value= "select Picture"/> ' + ' <input type= "file" name                                                = "Filelogo" class= "file" id= "style=" width:80%; "onchange=" UploadImages (This) "/>" + ' </div></td> ' + ' <td><a clAss= "del" href= "javascript:void (0)" > Delete </a></td></tr> "; 


In the method onchange= "UploadImages (This)" Here, we pass in the click Input to commit:



Multi-image upload function uploadimages (FileInput) {//var Isno = Filechange ($ (FileInput)); if (Isno = = undefined) {var options = {type: "POST", url: ').  /handler/ashxuploadfile.ashx?type=images ', Success:function (msg) {if (msg = = "Error")                        {clearimages ($ (FileInput));                    Showerrortip ("Upload failed");                            } else {if (msg! = "ErrorType") {clearimages ($ (FileInput));                            $ (FileInput). Parent (). Prev ("img"). attr ("src", Strpic + msg);                            Clearimages ($ (FileInput));                        $ (FileInput). attr ("Style", "");                    }}}, Error:function (msg) {clearimages ($ (FileInput));                    $ (FileInput). attr ("Style", ""); Showerrortip ("parameter exception! ");                }            };                           Pass the options to Ajaxform $ (' form '). Ajaxsubmit (options); //}         }



The problem was caused by ignoring the cleanup of input, adding the Clearimages function:



function Clearimages (selector) {            var fi;            var sourceparent;            if (selector) {                fi = $ (selector);                Sourceparent = Fi.parent ();            }            else {                return;            }            $ ("<form id= ' tempform ' ></form>"). AppendTo (document.body);            var tempform = $ ("#tempForm");            Tempform.append (FI);            Tempform.get (0). Reset ();            Sourceparent.append (FI);            Tempform.remove ();        }

in this way, the input is emptied using the temporary form.









Process the upload of images with form+input[type= "file" on the same page cannot empty the problem

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.