jQuery window.location.href 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18300674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:20:40  来源:igfitidea点击:

window.location.href not working

jqueryruby-on-railswindow.location

提问by Adam Zerner

My website is http://www.collegeanswerz.com/. I'm using rails. The code is for searching for colleges. I want the user to be able to type in the colleges name, click enter, and be taken to the url, rather than seeing the search results (if the user types in the name properly. if not I want to show the search results). I'm using "a" and "stackoverflow.com" as placeholders while I try to get it to work.

我的网站是http://www.collegeanswerz.com/。我正在使用导轨。该代码用于搜索大学。我希望用户能够输入大学名称,点击回车,然后被带到 url,而不是看到搜索结果(如果用户正确输入名称。如果没有,我想显示搜索结果) . 当我尝试让它工作时,我使用“a”和“stackoverflow.com”作为占位符。

I'm using window.location.href based on this: How to redirect to another webpage in JavaScript/jQuery?

我正在使用 window.location.href 基于这个:How to redirect to another web in JavaScript/jQuery?

javascript

javascript

$("#search_text").submit(function() {
    if ($("#search_field").val() == "a")
    {
        window.location.href = "http://stackoverflow.com";
        alert('worked');
    }
});

layout file

布局文件

<%= form_tag("/search", :method => 'get', :id => 'search_text', :class => 'form_search') do -%> 
    <div id="search"> <%= search_field_tag :search, params[:search], :placeholder => 'enter college', :id => "search_field", :class => 'input-medium search-query' %></div> 
<% end -%>

static_pages_controller.rb

static_pages_controller.rb

def search
  @colleges = College.search(params[:search])
end

The alert is working, which tells me that the things inside the if statement should be being executed. But it's taking me to the normal search results instead of stackoverflow.com. Why is this?

警报正在运行,它告诉我应该执行 if 语句中的内容。但它把我带到了正常的搜索结果而不是 stackoverflow.com。为什么是这样?

回答by SLaks

The browser is still submitting the form after your code runs.

代码运行后,浏览器仍在提交表单。

Add return false;to the handler to prevent that.

添加return false;到处理程序以防止这种情况。

回答by Arihant Godha

Try this

尝试这个

`var url = "http://stackoverflow.com";    
$(location).attr('href',url);`

Or you can do something like this

或者你可以做这样的事情

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

and add a return false at the end of your function call

并在函数调用结束时添加 return false

回答by Moumit

Please check you are using //not \\by-mistake , like below

请检查您使用的是//不是\\错误的,如下所示

Wrong:"http:\stackoverflow.com"

Right:"http://stackoverflow.com"

回答by Malek Tubaisaht

if anyone faced problem even after using return false;. then use the below.

如果有人在使用后遇到问题return false;。然后使用下面的。

setTimeout(function(){document.location.href = "index.php"},500);