【问题标题】:Redirect on deny of a confirm dialog拒绝确认对话框时重定向
【发布时间】:2017-03-07 10:44:31
【问题描述】:

我的任务是根据用户确认提交一份来自。如果用户确认,则提交表单。如果用户拒绝,则来自将重定向到其他页面。我在 jsp 中有一个这样的表单 -

.....
   <form id="stageUpdateForm" method="post">
   </form>
......

我正在尝试使用 javascript -

$('#stageUpdateForm').submit(function() {
  decision = confirm("Some data changed. Are you sure?");
  return decision;
  // if user deny then redirect 
});  

现在,如果用户拒绝从其停留在当前页面提交。但如果用户拒绝,我想将页面重定向到其他网址(例如 - www.google.com)。我该怎么做?

【问题讨论】:

    标签: javascript jquery forms redirect


    【解决方案1】:

    你想尝试这样的事情:

    if (decision) {
        window.location = "http://whenever.wherever";
    } else {
        // Returning false from a submit handler will prevent the submit action.
        return false;
    }
    

    【讨论】:

    • 如果用户确认,则提交表单。如果用户拒绝,则来自将重定向到其他页面(例如 - www.google.com)。
    • 好的,然后翻转代码块-原理适用:)
    【解决方案2】:

    你可以这样做:

    $('#stageUpdateForm').submit(function () {
        var decision = confirm('Some data changed. Are you sure?');
    
        // if user deny then redirect 
        if (!decision) {
            $(location).attr('href', 'http://www.google.com');
        }
    
        // If user confirm, than it will return true and the form will be sumbited
        return decision;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-18
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多