【问题标题】:Redirect using window.location doesn't work使用 window.location 重定向不起作用
【发布时间】:2015-02-27 18:42:36
【问题描述】:

我搜索了一个相关的帖子,但所有尝试都没有成功。

我的问题是,我正在尝试使用路径+查询字符串在 Javascript 中进行简单的重定向。我的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="submit" value="pesq" onclick="Redirect();"/>
    </div>
    </form>
    <script>
        function Redirect()
        {
          window.location.href = 'http://localhost:61267/Page1.aspx?q=name';            
        }
    </script>
</body>
</html>

window.location.href 始终设置为'http://localhost:61267/Page1.aspx'

我尝试过使用 window.location.search 但仍然没有成功。

我做错了什么?

【问题讨论】:

  • onclick="Redirect();" 替换为onclick="Redirect()"
  • 由于您不阻止事件的默认行为,我猜(目前无法测试)submit 操作触发的 redirect i>覆盖脚本的动作。
  • @theonlyguti,不起作用。
  • 我认为 t.niese 是对的;尝试onclick="Redirect(); return false;" 压缩默认提交。
  • @t.niese 完美运行!

标签: javascript redirect window.location


【解决方案1】:

改用 document.location。

      document.location = 'http://localhost:61267/Page1.aspx?q=name';  

【讨论】:

  • 像 Riad 一样,它对我不起作用,但还是谢谢。
【解决方案2】:

问题代码:

<form id="form1" runat="server">
<div>
<input type="submit" value="pesq" onclick="Redirect();"/>
</div>
</form>

HTML 元素,特别是表单,具有在用户操作发生时发生的默认事件。在您的上下文中,当您单击 input 框时,可能会调用 submit 事件。您需要通过从事件处理程序返回 false 或调用 preventDefault 来防止默认事件发生

<form onsubmit="Redirect(); return false;" id="form1" runat="server">
  <div>
    <input type="submit" value="pesq" onclick="Redirect(); return false;"/>
  </div>
</form>

附言

onsubmit 处理程序将允许您按回车键,并且仍然具有与单击 input 框时相同的效果。

【讨论】:

    【解决方案3】:

    不知道为什么你需要一个表单和提交按钮来进行重定向。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
    
        <div>
        <input type="button" value="pesq" onclick="Redirect();"/>
        </div>
    
            <script>
            function Redirect()
            {
              window.location.href = 'http://www.google.com';            
            }
        </script>
    </body>
    </html>
    

    我还注意到一些我可以通过添加解决的编码问题:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
    
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    

    【讨论】:

      【解决方案4】:

      只改变了输入类型,就完美了!感谢 t.niese。

      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication1.Page1" %>
      
      <!DOCTYPE html>
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
      </head>
      <body>
          <form id="form1" runat="server">
          <div>
          <input type="button" value="pesq" onclick="Redirect();return false;"/>
          </div>
          </form>
          <script>
              function Redirect()
              {
                  window.location.href = 'http://localhost:61267/Page1.aspx?q=name';                        
              }
          </script>
      </body>
      </html>
      

      【讨论】:

        【解决方案5】:

        您需要执行以下任一操作:

        1) 对于服务器端的asp代码:

        <asp:button text="Navigate" onclientclick="Redirect()" value="pesq" runat="server" />
        

        See MSDN Example

        2) 如果客户端代码:

        <input type="button" value="pesq" onclick="Redirect();"/> 
        

        【讨论】:

          猜你喜欢
          • 2011-08-02
          • 2020-11-13
          • 2011-12-14
          • 2019-02-03
          • 2016-12-16
          • 1970-01-01
          • 1970-01-01
          • 2020-06-21
          • 2018-07-02
          相关资源
          最近更新 更多