gpt4 book ai didi

jquery - jQuery点击事件可以结合回调吗?

转载 作者:行者123 更新时间:2023-12-01 07:57:23 25 4
gpt4 key购买 nike

我想显示带有“确定”按钮的确认弹出窗口。通常,如果我不想回调,那么“确定”按钮将隐藏弹出窗口和图层蒙版。当我回调然后单击“确定”按钮时,将在回调函数中执行某些操作。

function displayErrorPopup(message, callback) {
$("#error_popup_message").html(message);
display_popup($("#error_popup"));
if (callback && typeof (callback) === "function") {
$("#error_popup_ok").click(function() {
callback();
});
} else {
$("#error_popup_ok").click(function() {
hideErrorPopup();
});
}}

但是回调函数在调用时立即运行:

displayErrorPopup('error message', function(){
do something;
hideErrorPopup();});

没有单击“确定”按钮事件。

请帮帮我!!谢谢!

最佳答案

function displayErrorPopup(message, callback) {
$("#error_popup_message").html(message);

display_popup($("#error_popup"));

$("#error_popup_ok").click(function() {
if (typeof callback == "function") {
callback();
} else {
hideErrorPopup();
}
});
}

更简单的方法是:

function displayErrorPopup(message, callback) {
$("#error_popup_message").html(message);

display_popup($("#error_popup"));

$("#error_popup_ok").click(typeof callback == "function" ? callback : hideErrorPopup);
}

关于jquery - jQuery点击事件可以结合回调吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com