我正在玩 Buttons in the w3schools Tryit editor ,并且我正在尝试弄清楚如何在单击“取消”按钮时使我的浏览器重定向到 URL。
这是我尝试过的:
<form action="demo_form.asp" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<button type="submit" value="Submit">Submit</button>
<button type="reset" value="Reset">Reset</button>
<button type="cancel" onclick="javascript:window.location='http://stackoverflow.com';">Cancel</button>
</form>
但它不起作用。有什么想法吗?
最佳答案
cancel
不是类型属性的有效值,因此按钮可能默认为 submit
并继续提交表单。您可能是指 type="button"
。
(虽然 javascript:
应该被删除,虽然它不会造成任何伤害,但它完全没用 label )
虽然您没有任何类似按钮的功能,但最好使用:
<a href="http://stackoverflow.com"> Cancel </a>
…可能使用一些 CSS 使其看起来像一个按钮。
关于javascript - 如何创建重定向到 URL 的 HTML 取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18407832/