jQuery 如何让按钮将我的页面重定向到另一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16562577/
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 can I make a button redirect my page to another page?
提问by mattytommo
I have been trying the following:
我一直在尝试以下方法:
<form action="/home" class="inline">
<button class="float-left submit-button" >Home</button>
</form>
It seems to work but it goes to the page "/home?"
它似乎工作,但它转到页面“/home?”
Is there a better way for me to make a button inside a form make the page go to a new location?
有没有更好的方法让我在表单内创建一个按钮使页面转到新位置?
回答by mattytommo
Just add an onclick
event to the button
:
只需添加一个onclick
事件到button
:
<button onclick="location.href = 'www.yoursite.com';" id="myButton" class="float-left submit-button" >Home</button>
But you shouldn't really have it inline like that, instead, put it in a JS block and give the button
an ID:
但是你真的不应该像那样内联它,而是将它放在一个 JS 块中并给出button
一个 ID:
<button id="myButton" class="float-left submit-button" >Home</button>
<script type="text/javascript">
document.getElementById("myButton").onclick = function () {
location.href = "www.yoursite.com";
};
</script>
回答by Ehmad Imtiaz
try
尝试
<button onclick="window.location.href='b.php'">Click me</button>
回答by Ben Taylor
Just another variation:
只是另一个变化:
<body>
<button name="redirect" onClick="redirect()">
<script type="text/javascript">
function redirect()
{
var url = "http://www.(url).com";
window.location(url);
}
</script>
回答by Veer Shrivastav
This is here:
这是在这里:
<button onClick="window.location='page_name.php';" value="click here" />
回答by Muhammad Ramahy
you could do so:
你可以这样做:
<button onclick="location.href='page'">
you could change the action attribute of the form on click the button:
您可以在单击按钮时更改表单的操作属性:
<button class="float-left submit-button" onclick='myFun()'>Home</button>
<script>
myFun(){
$('form').attr('action','new path');
}
</script>