我正在尝试创建一个包含多个按钮(或链接)的页面,这些按钮(或链接)将定向到不同的页面。
我已使用以下代码进行了尝试,该代码应将用户重定向到 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>
但是,当我按下按钮时,页面会刷新而不是重定向我。
我在这里做错了什么?
最佳答案
将 type="button"
添加到按钮元素,以便它不会被视为表单的提交按钮(这会导致在事件处理程序执行任何操作之前提交表单)。
<button type="button" id="allSetsButton" class="float-left submit-button"> main</button>
关于javascript - php函数在表单内调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449504/