如何使用 jQuery 或仅使用 Javascript 将按钮重定向到另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2238368/
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
How to make a button redirect to another page using jQuery or just Javascript
提问by Ankur
I am making a prototype and I want the search button to link to a sample search results page.
我正在制作原型,我希望搜索按钮链接到示例搜索结果页面。
How do I make a button redirect to another page when it is clicked using jQuery or plain JS.
当使用 jQuery 或普通 JS 单击按钮时,如何使按钮重定向到另一个页面。
采纳答案by Quentin
Without script:
没有脚本:
<form action="where-you-want-to-go"><input type="submit"></form>
Better yet, since you are just going somewhere, present the user with the standard interface for "just going somewhere":
更好的是,因为你只是要去某个地方,所以向用户展示“只是去某个地方”的标准界面:
<a href="where-you-want-to-go">ta da</a>
Although, the context sounds like "Simulate a normal search where the user submits a form", in which case the first option is the way to go.
虽然,上下文听起来像“模拟用户提交表单的正常搜索”,在这种情况下,第一个选项是要走的路。
回答by Reigel
is this what you mean?
你是这个意思吗?
$('button selector').click(function(){
window.location.href='the_link_to_go_to.html';
})
回答by Darin Dimitrov
$('#someButton').click(function() {
window.location.href = '/some/new/page';
return false;
});
回答by g-francesca
In your html, you can add data attribute to your button:
在您的 html 中,您可以将 data 属性添加到您的按钮:
<button type="submit" class="mybtn" data-target="/search.html">Search</button>
Then you can use jQuery to change the url:
然后你可以使用 jQuery 来更改 url:
$('.mybtn').on('click', function(event) {
event.preventDefault();
var url = $(this).data('target');
location.replace(url);
});
Hope this helps
希望这可以帮助
回答by Sarfraz
With simple Javascript:
使用简单的 Javascript:
<input type="button" onclick="window.location = 'path-here';">
回答by Owen
No need for javascript, just wrap it in a link
不需要 javascript,只需将它包装在一个链接中
<a href="http://www.google.com"><button type="button">button</button></a>
回答by Vincent Ramdhanie
You can use:
您可以使用:
location.href = "newpage.html"
in the button's onclick event.
在按钮的 onclick 事件中。
回答by Gabriele Petrioli
This should work ..
这应该有效..
$('#buttonID').click(function(){ window.location = 'new url'});
回答by PetersenDidIt
You can use window.location
您可以使用 window.location
window.location="/newpage.php";
Or you can just make the form that the search button is in have a action of the page you want.
或者您可以让搜索按钮所在的表单具有您想要的页面的操作。
回答by shuseel
You can use this simple JavaScript code to make search button to link to a sample search results page. Here I have redirected to '/search'of my home page, If you want to search from Google search engine, You can use "https://www.google.com/search"in form action.
您可以使用这个简单的 JavaScript 代码使搜索按钮链接到示例搜索结果页面。这里我已经重定向到我主页的“/search”,如果你想从谷歌搜索引擎搜索,你可以在表单操作中使用“ https://www.google.com/search”。
<form action="/search"> Enter your search text:
<input type="text" id="searchtext" name="q">
<input onclick="myFunction()" type="submit" value="Search It" />
</form>
<script> function myFunction()
{
var search = document.getElementById("searchtext").value;
window.location = '/search?q='+search;
}
</script>

