我想在函数中调用模态。并根据点击模态的内容,我想继续我的功能。
我知道这可以通过不同的 promise 或回调来完成。但我不明白该怎么做
我的功能是
$(".delete").click(function(){
/// call a modal
$(".confirm").modal();
/*
now if clicked yes in modal
then continue with my ajax call
else return false
*/
});
我期待延迟的 promise 或回调答案。因为我想知道它们在这种情况下是如何工作的
最佳答案
试试这个
$(".confirm").dialog({
modal: true,
buttons: {
Yes: function () {
// Ajax call
$(this).dialog("close");
},
No: function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
<小时/>
编辑:这是一个带有“回调”的版本(不想添加新答案,因为这包括基础知识,只是稍微重构了):
function modalwithcallbacks(selector, yes, no, close)
{
$(selector).dialog({
modal: true,
buttons: {
Yes: yes,
No: no,
},
close: close
});
}
modalwithcallbacks(
".confirm",
function () { alert("yes"); $(this).dialog("close"); },
function () { alert("no"); $(this).dialog("close"); }
function () { $(this).remove(); }
);
下一步是将其设为 $.fn.
以删除选择器部分或直接在方法中添加对话框。
您可以选择要执行哪些操作作为回调(例如,仅"is")并保持关闭/否内联,给出:
$(".delete").click(function() {
modalwithcallbacks(".confirm", function() {
// yes clicked, continue with ajax call
});
});
关于javascript - 在函数 F1 内调用函数 F2。根据F2返回值继续F1执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543727/