jQuery jqModal 确认对话框回调问题
使用 jqModal 网站 FUN 部分的代码覆盖:
http://dev.iceburg.net/jquery/jqModal/#examples< br> (我编辑了代码以使用回调函数)
function confirm(msg,callback) {
$('#confirm')
.jqmShow()
.find('p.jqmConfirmMsg')
.html(msg)
.end()
.find(':submit:visible')
.click(function(){
if(this.value == 'yes')
(typeof callback == 'string') ?
window.location.href = callback :
callback();
$('#confirm').jqmHide();
});
}
$().ready(function() {
$('#confirm').jqm({overlay: 88, modal: true, trigger: false});
// trigger a confirm whenever links of class alert are pressed.
$('a.confirm').click(function() {
confirm('About to visit: '+this.href+' !',callbackfunction);
return false;
});
});
function callbackfunction()
{
console.log("callback triggered");
}
问题:每次调用确认函数时,回调都会逐渐触发,因此当我第二次单击时,处理程序会执行两次,第三次, 3次以此类推。
Using the code from the jqModal website, section FUN! Overrides:
http://dev.iceburg.net/jquery/jqModal/#examples
(I edited the code to make use of a callback function)
function confirm(msg,callback) {
$('#confirm')
.jqmShow()
.find('p.jqmConfirmMsg')
.html(msg)
.end()
.find(':submit:visible')
.click(function(){
if(this.value == 'yes')
(typeof callback == 'string') ?
window.location.href = callback :
callback();
$('#confirm').jqmHide();
});
}
$().ready(function() {
$('#confirm').jqm({overlay: 88, modal: true, trigger: false});
// trigger a confirm whenever links of class alert are pressed.
$('a.confirm').click(function() {
confirm('About to visit: '+this.href+' !',callbackfunction);
return false;
});
});
function callbackfunction()
{
console.log("callback triggered");
}
The problem: each time when the confirm function is called, the callback gets triggered incrementally, so when I click the 2nd time, the handler gets executed 2 times, the 3th time, 3 times and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为每次调用确认函数时,都会添加一个新的“单击”事件处理程序。
This happens because each time you call the confirm function, you add a new 'click' event handler.
解决方案只是在绑定新的、更新的回调之前取消绑定确认框中的单击事件。在下面的示例中,请注意在绑定新回调之前的确认()函数以及任何绑定的点击事件的取消绑定。也许他的示例代码确实应该是:
自您最初的帖子以来已经过去了一段时间,但也许这对将来的某人有用。 jqModal 确实非常棒。
The solution is simply unbinding the click event in the confirm box before binding the new, updated callback. In my example below, note the confirm() function and the unbind of any bound click events before binding the new callback. Perhaps his example code should really be:
Some time has past since your original post but perhaps this can be of use to someone in the future. jqModal is really quite great.