1

我有两个按钮“保存”和“关闭”。我需要关闭按钮来询问用户是否要在重定向回主页之前保存。下面的代码显示了我想要完成的事情。

$('#closeButton').click(function() {
  if(confirm("CloseWarning")) {
    $('#saveButton').click();
    window.location.href = "main.html";
  } else {
    window.location.href = "main.html";
  }
});

但是问题是 saveButton 单击事件在整个页面重定向之前没有足够的时间完成。所以我的问题是在这种情况下进行回调的最佳方法是什么,以便单击保存按钮,然后我们重定向到主页。

4

3 回答 3

2

假设您的保存按钮调用了一个 javascript 函数,您可以执行以下操作:

function saveFunction(doRedirect){
    //normal save code
    if(doRedirect)
        window.location.href = "main.html";
}

注意:如果您的“正常保存代码”是 AJAX 调用,则应doRedirect在 ajax 请求的成功回调中添加检查。

然后你可以像这样使用:

$('#closeButton').click(function() {
  if(confirm("CloseWarning")) {
    saveFunction(true);
  } else {
    window.location.href = "main.html";
  }
});

使用您自己的回调的替代方法:

function saveFuncton(callback){
   //normal save code

   //when done (or on AJAX success)
   if(callback)
      callback(true);//or false if not successful
}

接着....

$('#closeButton').click(function() {
  if(confirm("CloseWarning")) {
    saveFunction(function(success){
       if(success){
          window.location.href = "main.html";
       }
    });
  } else {
    window.location.href = "main.html";
  }
});

HACKY 方法(除非您绝对没有其他替代方案,否则请勿使用)

var isSaving = false;
$(document).ajaxStop(function () {
    if (isSaving) {
        window.location.href = "main.html";
    }
});

接着...

$('#closeButton').click(function() {
  if(confirm("CloseWarning")) {
    isSaving = true;
    $('#saveButton').click();
  } else {
    window.location.href = "main.html";
  }
});

这可能有效,但它实际上取决于您的保存功能如何工作,以及是否有其他任何东西可能同时进行 ajax 调用。此外,如果您保存函数执行多个 AJAX 调用,则此方法可能会失败。这是一种 hacky 方法,使用它需要您自担风险!

于 2013-05-15T17:11:17.583 回答
1

您可以在触发事件时将数据传递给事件,因此您可以执行以下操作:

$('#closeButton').click(function(e) {
    if(confirm('..')) {
         $('#saveButton').trigger('click', [true]);
    } else {
         window.location.href = "main.html";
    }
});

$('#saveButton').click(function(e, close) {
    // do saving stuff ..
    // for normal clicks "close" will be undefined, otherwise close when done
    if(close) {
        window.location.href = "main.html";
    }
});

如果你想更一点,你可以让 saveButton 中的关闭代码是一个类似于 closeButton 的触发器,带有一个force强制关闭的变量,所以你不会在两个不同的部分重复重定向 URL编码。

如果你不能修改你的保存逻辑(这似乎是一个有问题的事情,但是好的......)你可以这样做,假设保存逻辑由一个 AJAX 调用组成并且你没有任何其他 AJAX 请求被触发. 如果你这样做了,你可以settings在关闭之前检查变量以确保它是正确的:

$('#closeButton').click(function(e) {
    if(confirm('..')) {
         $(document).ajaxComplete(function(e, xhr, settings) {
             window.location.href = "main.html";
         });
         $('#saveButton').click();
    } else {
         window.location.href = "main.html";
    }
});
于 2013-05-15T17:14:28.987 回答
0

无论在单击 saveButton 时执行什么脚本,都在保存表单时将响应设置在变量中。然后检查它的值。这是示例:

$('#closeButton').click(function() {
  if(confirm("CloseWarning")) {
    $('#saveButton').click(function(){
         // Code to save the form 
         if(saved == true){
              window.location.href = "main.html";
         }
    });
  } else {
    window.location.href = "main.html";
  }
});
于 2013-05-15T17:12:05.313 回答