【问题标题】:Redirecting in ASP.NET MVC using Javascript or something else the best way?使用 Javascript 或其他最佳方式在 ASP.NET MVC 中重定向?
【发布时间】:2010-07-07 16:37:00
【问题描述】:

我有一个确认框并重定向到一个操作,但它不起作用..

<script type="text/javascript">


  function quitProgram()
    {
        var answer = confirm("Are you sure you want to quit your current program?");
        if (answer)
            window.location("http://www.google.com");
        else
            window.location("http://www.yahoo.com");
     }
    </script>

HTML代码-

<input style="float:right;" type="submit" value="Quit Program" id="QuitProgram" onclick="quitProgram()" />

但是重定向永远不会发生...任何人都可以帮助我解决这个问题...最终我想做的是重定向到基于用户响应的操作...如果有人让我知道最好的方式我会很棒应该这样做吗?

【问题讨论】:

    标签: javascript asp.net-mvc-2 redirect


    【解决方案1】:

    window.location 是属性而不是方法:

    if (answer)
        window.location = "http://www.google.com";
    else
        window.location = "http://www.yahoo.com";
    

    【讨论】:

    • 很好.. 有时就像是脑筋急转弯,看起来正确但语法完全错误。
    • 是的...我的错...谢谢杰夫...实际上提交是一个问题..当我将其更改为按钮时它起作用了..
    【解决方案2】:

    Stuart identified one problem 使用问题中发布的代码。您还需要做的另一件事是将input 元素的类型从“提交”更改为“按钮”,否则提交将导致帖子覆盖重定向。

    【讨论】:

      【解决方案3】:

      我不确定,但试试这个

      function quitProgram()
      {
          var answer = return confirm("Are you sure you want to quit your current program?");
          if (answer)
              window.location("http://www.google.com");
          else
              window.location("http://www.yahoo.com");
       }
      

      【讨论】:

        猜你喜欢
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 2013-06-28
        相关资源
        最近更新 更多