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

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

How can I make a button redirect my page to another page?

javascriptjqueryhtml

提问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 onclickevent 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 buttonan 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>