jQuery change method on input type=“file”

后端 未结 3 687
北荒
北荒 2020-12-02 11:56

I\'m trying to embrace jQuery 100% with it\'s simple and elegant API but I\'ve run into an inconsistency between the API and straight-up HTML that I can\'t quite figure out.

相关标签:
3条回答
  • 2020-12-02 12:18

    is the ajax uploader refreshing your input element? if so you should consider using .live() method.

     $('#imageFile').live('change', function(){ uploadFile(); });
    

    update:

    from jQuery 1.7+ you should use now .on()

     $(parent_element_selector_here or document ).on('change','#imageFile' , function(){ uploadFile(); });
    
    0 讨论(0)
  • 2020-12-02 12:21

    I could not get IE8+ to work by adding a jQuery event handler to the file input type. I had to go old-school and add the the onchange="" attribute to the input tag:

    <input type='file' onchange='getFilename(this)'/>
    
    function getFileName(elm) {
       var fn = $(elm).val();
       ....
    }
    

    EDIT:

    function getFileName(elm) {
       var fn = $(elm).val();
       var filename = fn.match(/[^\\/]*$/)[0]; // remove C:\fakename
       alert(filename);
    }
    
    0 讨论(0)
  • 2020-12-02 12:24

    As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). Refer: http://api.jquery.com/on/

    $('#imageFile').on("change", function(){ uploadFile(); });
    
    0 讨论(0)
提交回复
热议问题