简体   繁体   English

javascript重定向无法正常工作

[英]javascript redirect isnt working

I have a form that takes a users input and redirects to a the window to a URL with their input appended to the end. 我有一个接受用户输入并重定向到URL的窗口的表单,其输入附加到末尾。

Here is my HTML 这是我的HTML

        <form id="wikiForm">
            <label id="sideBarLabel">VoIP Services
                <input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
            </label>
            <input type="submit" value="Search" onclick="searchWiki();" />
        </form>

The javascript it runs 它运行的JavaScript

function searchWiki() {
    alert("Form Works!");
    var siteQuery = $('#query-string').val();
    window.location.href = "http://wiki.voipinnovations.com/dosearchsite.action?queryString=" + siteQuery;
    alert("SECOND MESSAGE");
}

The issue is that it does not redirect. 问题是它不会重定向。 It only appends the 'siteQuery' variable to the end of the current URL. 只会将'siteQuery'变量附加到当前URL的末尾。 I know its calling the javascript because I see both alerts. 我知道它正在调用javascript,因为我看到了这两个警报。 I'm not sure what I'm doing wrong here. 我不确定我在做什么错。

The issue is due to the fact that you're actually submitting your form, and the redirection is lost as the form submission occurs first. 该问题是由于您实际上是在提交表单而导致的,因为首先提交表单,所以重定向丢失了。 There are two easy ways to fix this: 有两种简单的方法可以解决此问题:

  1. Change the type of the input from submit to button , OR 将输入的类型从submit更改为button ,或者
  2. Stop the submission of the form by returning false from your function and changing the call of the function to onclick="return searchWiki();" 通过从函数返回false并将函数调用更改为onclick="return searchWiki();"停止提交表单onclick="return searchWiki();"

jsFiddle example (1) jsFiddle示例 (1)

jsFiddle example (2) jsFiddle示例 (2)

There reason is because you using type="submit" , which submits and sends an GET header to the default action parameter (current page). 原因是因为您使用type="submit" ,它会提交GET头并将其发送到默认action参数(当前页面)。

Change the type="submit" to type="button" . type="submit"更改为type="button"

<form id="wikiForm">
    <label id="sideBarLabel">VoIP Services
        <input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
    </label>
    <input type="button" value="Search" onclick="searchWiki();" />
</form>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
    function searchWiki() {
        alert("Form Works!");
        var siteQuery = $('#query-string').val();
        alert(siteQuery);
        window.location.assign("http://wiki.voipinnovations.com/dosearchsite.action?queryString=" + siteQuery);
        alert("SECOND MESSAGE");
    }
</script>

I tried the code with type="submit" and it's alerting, but not redirecting, because the submit is prioritized before the window.location change, thats the reason it just appends a ?queryString=value to the current url. 我用type="submit"尝试了代码,它很警报,但没有重定向,因为提交是在window.location更改之前确定优先级的,这就是它只将?queryString=value附加到当前url的原因。 If you change the type like showed in the code above, it's working perfectly. 如果您像上面的代码所示那样更改类型,则它工作正常。

Can't you just use assign? 你不能只使用assign吗?

window.location.assign("http://wiki.voipinnovations.com/dosearchsite.action?queryString=" + siteQuery);

Check out: http://www.w3schools.com/js/js_window_location.asp 检出: http : //www.w3schools.com/js/js_window_location.asp

Use default action and method attributes instead 改用默认actionmethod属性

The HTML form element provides the mechanism for doing this out of the box. HTML表单元素提供了开箱即用的机制。

<form id="wikiForm" action="http://wiki.voipinnovations.com/dosearchsite.action" method="GET">
    <label id="sideBarLabel">VoIP Services
        <input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
    </label>
    <input type="submit" value="Search" />
</form>

But, if you must use javascript, make this change: 但是,如果必须使用javascript,请进行以下更改:

From: 从:

window.location.href = "…";

To: 至:

window.location.assign("…"); // or
window.location = "…"

This is because location.href is a read-only property and location.assign() is the proper method for setting the new location to be loaded. 这是因为location.href是只读属性,而location.assign()是设置要加载的新位置的正确方法。 You may also directly assign a string to the location object: 您也可以直接为location对象分配一个字符串:

Whenever a new value is assigned to the location object, a document will be loaded using the URL as if location.assign() had been called with the modified URL. 每当将新值分配给位置对象时,都将使用URL加载文档,就像使用修改后的URL调用location.assign()一样。

Source: MDN 资料来源: MDN

Change input type=submit to type=button 将输入类型=提交更改为类型=按钮

http://plnkr.co/edit/w4U7Sbm3XSKN8j3zUFMe?p=preview http://plnkr.co/edit/w4U7Sbm3XSKN8j3zUFMe?p=preview

<form id="wikiForm">
    <label id="sideBarLabel">VoIP Services
    <input type="text" placeholder="Search Wiki: e.g. E911" name="queryString" id="query-string" />
    </label>
    <input type="button" value="Search" onclick="searchWiki();" />
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM