使用 jQuery 绑定,顺序错误?
我有一个加载内容的脚本,然后应该绑定删除链接以删除内容。
但是我发现它没有与删除链接绑定,即使我将代码放入应该重新绑定的函数中。
$(document).ready(function() {
function loadImages() {
$.get('/management/image/get/site_id/1/area/News', function(data) {
$("#existing-images").html(data);
});
$(".deleteImage").click(function() {
id = $(this).attr('id'); id = id.split('_');
alert(id);
/*$.post(second_url+'id/'+id, '', function(theResponse) {
$("#image_"+id+"").remove();
});*/
});
}
$("#fileInput").uploadify({
'uploader' : '/library/jquery/uploadify/uploadify.swf',
'script' : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
'cancelImg' : '/library/jquery/uploadify/cancel.png',
'folder' : '/images/Image/',
'multi' : true,
'onAllComplete' : function(e, queueId, file, response, data) {
$('#fileInput').uploadifyClearQueue();
loadImages();
},
});
loadImages();
});
I have this script that loads in content, and should then bind the delete link to remove the content.
However I'm finding that it isnt binding with the delete link, even then I put the code in a function that should rebind.
$(document).ready(function() {
function loadImages() {
$.get('/management/image/get/site_id/1/area/News', function(data) {
$("#existing-images").html(data);
});
$(".deleteImage").click(function() {
id = $(this).attr('id'); id = id.split('_');
alert(id);
/*$.post(second_url+'id/'+id, '', function(theResponse) {
$("#image_"+id+"").remove();
});*/
});
}
$("#fileInput").uploadify({
'uploader' : '/library/jquery/uploadify/uploadify.swf',
'script' : '/gallery/image/area/site_id/<?php echo $site->id; ?>/area/<?php echo $this->area; ?>',
'cancelImg' : '/library/jquery/uploadify/cancel.png',
'folder' : '/images/Image/',
'multi' : true,
'onAllComplete' : function(e, queueId, file, response, data) {
$('#fileInput').uploadifyClearQueue();
loadImages();
},
});
loadImages();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 $.live 函数动态绑定删除链接,而不是每次创建时都这样做要求。
尝试类似的东西:
You may use the $.live function to dynamically bind delete links instead of doing it every time you make an request.
try something like:
您正在 ajax 请求完成之前绑定删除链接。
you are binding the delete link before ajax request complete.
这是因为 ajax 调用的异步性质。
您编写的代码尝试将单击事件绑定到尚未注入 DOM 的按钮
尝试将代码更改为:
将绑定移动到
$.get
调用的成功回调中使得确保在绑定发生之前已注入新的 html。或者尝试使用 delegate 而不是绑定到元素,这样就不需要重新绑定一直
It's because of the asynchronous nature of the ajax call.
The code you have writen tries to bind the click event to a button that has not yet been injected into the DOM
Try changing your code to:
Moving the binding into the success callback of the
$.get
call makes sure that the new html has been injected before the binding occurs.Alternativly try looking into using delegate instead of binding to the element, that way you don't need to rebind all the time