php功能在表单内调用

时间:2015-12-24 08:17:03

标签: javascript php forms

我正在尝试创建一个包含多个按钮(或链接)的页面,这些按钮将指向不同的页面。

我尝试使用以下代码将用户重定向到google.com。

<!DOCTYPE HTML>
<html>
<body>

<form id='form' action="main.php">

    <button id="allSetsButton" class="float-left submit-button"> main</button>
</form>

<script type="text/javascript">

        document.getElementById("allSetsButton").onclick = function (){
location.href = 'http://google.com';
};
</script>

</body>
</html>

然而,当我按下按钮时,页面会刷新而不是重定向。

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

考虑到你的要求,你为什么不使用普通链接并根据需要设计样式?

<!DOCTYPE HTML>
<html>
<style>
  .submit-button {
    display: inline-block;
    padding: 0.5em 1em;
    background-color: #e1ecf4;
  }
</style>
<body>
<form id='form' action="main.php">
  <a href="http://google.com" class="float-left submit-button">Main</a>
</form>
</body>
</html>

答案 1 :(得分:1)

type="button"添加到您的按钮元素,以便它不被视为表单的提交按钮(这会导致在您的事件处理程序可以执行任何操作之前提交表单)。

<button type="button" id="allSetsButton" class="float-left submit-button"> main</button> 

答案 2 :(得分:1)

你可以试试这个

var ERROR = "Received error: %s";

function reportError (err) {
    reporter.log(ERROR, err);
}

或者简而言之

<!DOCTYPE HTML>
<html>
<body>

<form id='form' action="main.php">

<button type="button" id="allSetsButton" class="float-left submit-button" onClick="gotoweb()"> main</button>
</form>

<script type="text/javascript">

    function gotoweb(){
        window.location.href = 'http://google.com';
        };
</script>

</body>