【发布时间】: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