How to handle onchange event on input type=file in

2019-01-18 00:55发布

The code is:

<input ID="fileUpload1" runat="server" type="file"

The following works fine:

<input onchange="javascript:alert('hola');" ID="fileUpload1"  runat="server" type="file"

I'd like to get this result using jQuery, but that doesn't work:

$('#fileUpload1').change(function (e) {
    alert("hola");
});

I am missing something? (Edit: Yes I missed include the *.js file.)

6条回答
等我变得足够好
2楼-- · 2019-01-18 01:40

This jsfiddle works fine for me.

$(document).delegate(':file', 'change', function() {
    console.log(this);
});

Note: .delegate() is the fastest event-binding method for jQuery < 1.7: event-binding methods

查看更多
趁早两清
3楼-- · 2019-01-18 01:48

Or could be:

$('input[type=file]').change(function () {
    alert("hola");
});

To be specific: $('input[type=file]#fileUpload1').change(...

查看更多
老娘就宠你
4楼-- · 2019-01-18 01:50

Try to use this:

HTML:

<input ID="fileUpload1" runat="server" type="file">

JavaScript:

$("#fileUpload1").on('change',function() {
    alert('Works!!');
});

查看更多
何必那么认真
5楼-- · 2019-01-18 01:54

It should work fine, are you wrapping the code in a $(document).ready() call? If not use that or use live i.e.

$('#fileupload1').live('change', function(){ 
    alert("hola");
});

Here is a jsFiddle of this working against jQuery 1.4.4

查看更多
▲ chillily
6楼-- · 2019-01-18 01:55
 $('#fileupload').bind('change', function (e) { //dynamic property binding
alert('hello');// message you want to display
});

You can use this one also

查看更多
Anthone
7楼-- · 2019-01-18 01:56

Demo : http://jsfiddle.net/NbGBj/

$("document").ready(function(){

    $("#upload").change(function() {
        alert('changed!');
    });
});
查看更多
登录 后发表回答