如何在jQuery中处理input type = file的onchange事件?
的代码是:如何在jQuery中处理input type = file的onchange事件?
<input ID="fileUpload1" runat="server" type="file"
下正常工作:
<input onchange="javascript:alert('hola');" ID="fileUpload1" runat="server" type="file"
我想获得使用jQuery这样的结果,但是,这并不工作:
$('#fileUpload1').change(function (e) {
alert("hola");
});
我错过了什么? (编辑:是的,我错过了包括* .js文件)。
$("document").ready(function(){
$("#upload").change(function() {
alert('changed!');
});
});
尝试使用此:
HTML:
<input ID="fileUpload1" runat="server" type="file">
的JavaScript:
$("#fileUpload1").on('change',function() {
alert('Works!!');
});
错误的事件和错误的ID。 – Esailija 2012-08-08 08:41:32
对不起,我没有正确地看到问题...现在检查@Esailija – 2012-08-08 08:54:29
它应该w奥克罚款,你打电话给$(document).ready()
电话的代码?如果不使用或使用live
即
$('#fileupload1').live('change', function(){
alert("hola");
});
下面是这个工作的jsFiddle反对的jQuery 1.4.4
这jsfiddle工作正常,我。
$(document).delegate(':file', 'change', function() {
console.log(this);
});
注:.delegate()是jQuery的< 1.7最快的事件结合方法:event-binding methods
或者可能是:
$('input[type=file]').change(function() {
alert("hola");
});
具体到:$('input[type=file]#fileUpload1').change(...
$('#fileupload').bind('change', function (e) { //dynamic property binding
alert('hello');// message you want to display
});
您可以使用这其中也
元素的ID是' “fileUpload1”''没有 “fileupload1”'。你是否也在元素存在之后运行你的代码,并且你包含了jQuery等?同时检查您的浏览器控制台是否有javascript错误 – Esailija 2012-08-08 08:40:13
我搜索了类似的关键字,但我在寻找“没有jQuery”版本... – 2016-10-14 05:30:55