Javascript/Jquery Callback for Fancybox 是 否 确认
Javascript/Jquery Callback for Fancybox Yes No Confirm
我正在尝试实现一个花哨的"是否"确认,但我在根据需要使回调工作时遇到问题。下面的示例使用 Fancybox v2。
下面代码的问题在于,单击"测试"时HREF
调用函数"do_something",当用户单击"测试"时未调用#fancyConfirm_ok
。
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"回调被调用两次 - 在打开之前和关闭之后(这将在下一个版本中更改)。
我会在链接上绑定一个点击事件,并在用户单击其中一个按钮时调用回调,例如 - http://jsfiddle.net/xcJFY/1/
不是双回调方法的忠实粉丝(对不起 Janis)。
因此,在尝试找到解决方案时,我自己玩了类似的代码,发现只要我的返回值没有在函数范围内定义,它就可以正常工作,当我的返回值在函数内定义时,我会得到奇怪的行为,它会工作一段时间然后恢复为未定义。
实际上,返回值需要在函数、全局、闭包等范围之外。
$(document).ready(function() {
$(".fancybox").on("click", function(e){
e.preventDefault();
confirm("Do you wish to continue?", true, function(resp) {
alert("You clicked " + resp);
});
});
});
function confirm(msg, modal, callback) {
$.fancybox("#confirm",{
modal: modal,
beforeShow: function() {
$(".title").html(msg);
},
afterShow: function() {
$(".confirm").on("click", function(event){
if($(event.target).is(".yes")){
ret = true;
} else if ($(event.target).is(".no")){
ret = false;
}
$.fancybox.close();
});
},
afterClose: function() {
callback.call(this, ret);
}
});
}
这是一个 jsfiddle 示例
感谢弗朗西斯科·迪亚兹(Francisco Diaz)在Google Groups FancyBox论坛上的帖子,为我指明了正确的方向。
更新:
现在扩展了我的示例以使用动态构建的按钮和自定义返回值,因此您不仅限于 true 和 false。
$(document).ready(function() {
$(".fancybox").on("click", function(e){
e.preventDefault();
confirm("Do you wish to continue?", {
/*
Define your buttons here, can handle multiple buttons
with custom title and values
*/
buttons: [
{ class: "yes", type: "button", title: "Yes", value: "yes" },
{ class: "no", type: "button", title: "No", value: "no" },
{ class: "maybe", type: "button", title: "Maybe?", value: "maybe" }
],
modal: true
},
function(resp) {
alert("You clicked " + resp);
}
);
});
});
function confirm(msg, options, callback) {
$.fancybox("#confirm",{
modal: options.modal,
beforeShow: function() {
this.content.prepend("<p class='"title'"></p>");
$(".title").html(msg);
for (i = 0; i < options.buttons.length; i++) {
this.content.append($("<input>", {
type: "button", class: "confirm " + options.buttons[i].class,
value: options.buttons[i].title
}).data("index", i));
}
},
afterShow: function() {
$(".confirm").on("click", function(event){
ret = options.buttons[$(event.target).data("index")].value;
$.fancybox.close();
});
},
afterClose: function() {
this.content.html("");
callback.call(this, ret);
}
});
}
添加了 jsfiddle 示例
相关文章:
- Fancybox是否将Click事件静音
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 按钮字段确认点击不'不起作用
- 删除确认对话框在第一次单击时不起作用
- 嵌套到另一个函数中的Fancybox函数;不起作用
- 如何在用户按下F5键或单击浏览器时显示自定义的fancybox's后退/关闭按钮
- 在表单完成并确认密码之前,请禁用提交按钮
- 动态加载fancybox并将其连接
- 在asp.net中确认是或否消息
- javascript确认对话框有时会不断出现
- 是否可以用变量确认提示警报
- 检测在确认页面导航时单击了哪个按钮
- 当有许多形式时,Fancybox脚本不起作用
- 确认前执行
- 如何为javascript方法放入jquery确认对话框
- Modify fancybox html
- Javascript/Jquery Callback for Fancybox 是 否 确认
- 如何在fancybox中点击关闭按钮返回确认
- 使用fancybox扩展Javascript确认框
- 如何让fancybox显示一个“是/否确认框”?通过href onclick