你好,我刚刚编写一个简单的代码,因为我学的jQuery这是代码jQuery的活不工作,代码简单
$(document).ready(function(){
$('input[type="file"]').live({
change : function(){
alert('ok');
}
});
});
,但它不希望工作是什么问题?
你好,我刚刚编写一个简单的代码,因为我学的jQuery这是代码jQuery的活不工作,代码简单
$(document).ready(function(){
$('input[type="file"]').live({
change : function(){
alert('ok');
}
});
});
,但它不希望工作是什么问题?
现场已被弃用试试这个:
$(document).ready(function(){
$(document).on('change','input[type="file"]',function(){
alert('ok');
});
});
可以使用。对()生活(。 )已弃用。
$(document).ready(function(){
$(document).on('change','input[type="file"]',function(){
alert('ok');
});
});
thx我没有但它并没有像现实中看到我的后续代码那样做同样的工作$(document).ready(function(){('。duplicated')。on(“click”,function(){} {alert '; ok'); $(this).after(''); }); }); – user2174729 2013-03-20 17:37:52
@ user2174729我更新了我的code.check它一次 – PSR 2013-03-20 17:43:30
jQuery的live()
documentation状态:
在jQuery 1.7的,所述.live()方法被弃用。使用.on()连接到事件处理程序 。老版本jQuery的用户应该优先使用 .delegate(),而不是.live()。
使用on()
(documentation)绑定您的活动(如果不是动态添加的元素):
$('input[type="file"]').on('change',function(){
//Stuff
});
注:这不正是同样的事情$('input[type="file"]').change()
如果您使用live()
将函数绑定到动态添加的DOM元素,您应该使用on()
类似于thi s:
$(document).on('change', 'input[type="file"]', function(){
//Stuff
});
这就是我的意思http://jsfiddle.net/7Ny57/1/每次我点击按钮它将apear警报与确定并再次复制buootn当我按它仍然会执行相同的工作 – user2174729 2013-03-20 17:42:32
@ user2174729您的小提琴没有包含jQuery。 **你的网页上是否包含了jQuery?**请再次阅读我的回答,特别是关于**动态添加**元素的部分,并检查[fiddle](http://jsfiddle.net/bigood/9xjQW/1 /)作为你的修正 – Bigood 2013-03-20 17:44:55
你使用的是什么版本的jQuery? '.live()'在1.7中被弃用。 – j08691 2013-03-20 17:33:14
更不用说完全在1.9中删除了。 – ajp15243 2013-03-20 17:37:30
注意,如果你的文件绑定,你不需要'$(document).ready(' – 2013-03-20 17:50:05