我正在尝试实现一个花哨的 Yes No confirm 并且我在让回调按需要工作时遇到了问题。下面的示例使用 Fancybox v2 .
下面代码的问题在于,当单击 HREF
“Test”时调用函数“do_something”,而当用户单击 #fancyConfirm_ok
时未调用函数“do_something” .
function fancyConfirm(msg,callback) {
var ret;
jQuery.fancybox({
'modal' : true,
'content' : "<div style=\"margin:1px;width:240px;\">"+msg+"<div style=\"text-align:right;margin-top:10px;\"><input id=\"fancyconfirm_cancel\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Cancel\"><input id=\"fancyConfirm_ok\" style=\"margin:3px;padding:0px;\" type=\"button\" value=\"Ok\"></div></div>",
'afterShow' : function() {
jQuery("#fancyconfirm_cancel").click(function() {
ret = false;
//jQuery.fancybox.close();
$.fancybox.close();
})
jQuery("#fancyConfirm_ok").click(function() {
ret = true;
jQuery.fancybox.close();
})
},
'afterClose' : function() {
if (typeof callback == 'function'){
callback.call(this, ret);
} else {
var callback_type = typeof callback;
alert(callback_type);
}
}
});
}
<a href="#" onclick="fancyConfirm('Are you sure you want to delete?', do_something('a', 'b') );return false;">Test</a>
最佳答案
只要用户点击链接,您的方法“do_something('a', 'b')”就会执行,返回的结果将作为第二个参数传递给“fancyConfirm”方法。这就是为什么您的参数“回调”总是“未定义”的原因。
fancyBox 也有一个问题 - “afterClose”回调被调用两次 - 在打开之前和关闭之后(这将在下一个版本中更改)。
我会在链接上绑定(bind)一个点击事件,并在用户点击其中一个按钮时调用回调,比如 - http://jsfiddle.net/xcJFY/1/
关于Fancybox 的 Javascript/Jquery 回调 是 否 确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10446122/