Javascript 方法多次调用
我的目的是用jquery定义ajax方法回调。 父div的所有DOM都是动态加载的 我已经为每个评论定义了删除按钮 在抛出ajax请求之前,我想从twitter bootrap modal获得确认:Javascript 方法多次调用,javascript,jquery,ajax,Javascript,Jquery,Ajax,我的目的是用jquery定义ajax方法回调。 父div的所有DOM都是动态加载的 我已经为每个评论定义了删除按钮 在抛出ajax请求之前,我想从twitter bootrap modal获得确认: this.deleteComment=函数(){ self=这个; $(“按钮[name='deleteComment'])。每个(函数(索引,el){ $(this).unbind().bind('click',函数(事件){ event.preventDefault(); var ref=$(t
this.deleteComment=函数(){
self=这个;
$(“按钮[name='deleteComment'])。每个(函数(索引,el){
$(this).unbind().bind('click',函数(事件){
event.preventDefault();
var ref=$(this.attr('data-ref');
var callback=函数(){
/*$.ajax({
url:“/path/to/file”,
键入:“删除”,
数据:{param1:'value1'},
})
.done(函数(){
控制台日志(“成功”);
})
.fail(函数(){
控制台日志(“错误”);
});*/
警报(“多个”);
}
self.confirm(“删除注释”),“确定要删除吗?”\
若要删除此评论,请单击“”,然后单击“回调”);
});
});
}
this.confirm=函数(标题、问题、回调){
$(“.modal title”).html(head);
var body=$(“”)文本(问题);
$(“.modal body”).html(body);
$(“#删除模式”)。单击(函数(事件){
回调();
$('.customModal').modal('hide');
});
$('.customModal').modal('show');
}
每次调用confirm()
方法时,您都在绑定click事件deletemodel
元素!
因此,您必须像在deletecoment
按钮上那样再次unbind()
和bind()
像这样:
this.confirm=函数(标题、问题、回调){
$(“.modal title”).html(head);
var body=$(“”)文本(问题);
$(“.modal body”).html(body);
$(“#deleteModal”).unbind().bind('click',函数(事件){
回调();
$('.customModal').modal('hide');
});
$('.customModal').modal('show');
}
为什么不直接使用事件delegation@ArunPJohny您可以投票选择“复制”而不是“离题”
this.deleteComment = function () {
self = this;
$("button[name='deleteComment']").each(function (index, el) {
$(this).unbind().bind('click', function (event) {
event.preventDefault();
var ref = $(this).attr('data-ref');
var callback = function () {
/*$.ajax({
url: '/path/to/file',
type: 'DELETE',
data: {param1: 'value1'},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
});*/
alert("multiple");
}
self.confirm("Delete Comment ", "Are you sure you want \
to delete this Comment ?", callback);
});
});
}
this.confirm = function(head, question, callback) {
$(".modal-title").html(head);
var body = $("<p/>").text(question);
$(".modal-body").html(body);
$("#deleteModal").click(function(event) {
callback();
$('.customModal').modal('hide');
});
$('.customModal').modal('show');
}