如何创建一个重定向到URL的HTML取消按钮 [英] How to create a HTML Cancel button that redirects to a URL

查看:152
本文介绍了如何创建一个重定向到URL的HTML取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩w3schools Tryit编辑器中的按钮,我试图弄清楚如何让我的浏览器重定向到一个URL,当我点击取消按钮。



以下是我尝试过的内容:

 < form action =demo_form.aspmethod =get>名字:< input type =textname =fname>< br>姓氏:< input type =textname =lname>< br> <按钮类型=提交值=提交>提交< /按钮> <按钮类型=重置值=重置>重置< /按钮> < button type =cancelonclick =javascript:window.location ='http://stackoverflow.com';>取消< / button>< / form>  



但它不起作用。任何想法?

解决方案

取消不是有效的类型值属性,所以该按钮可能默认为 submit 并继续提交表单。您可能意指 type =button



javascript:应该被删除,虽然它没有任何伤害,但它是完全没有用的标签



虽然没有类似按钮的功能,但最好使用:

 < a href =http://stackoverflow.com>取消< / a> 

...可能带有一些CSS使它看起来像一个按钮。


I am playing with the Buttons in the w3schools Tryit editor, and I am trying to figure out how to make my browser redirect to an URL when I click on the "Cancel" button.

Here's what I have tried:

<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>

But it doesn't work. Any ideas?

解决方案

cancel is not a valid value for a type attribute, so the button is probably defaulting to submit and continuing to submit the form. You probably mean type="button".

(The javascript: should be removed though, while it doesn't do any harm, it is an entirely useless label)

You don't have any button-like functionality though, so would be better off with:

<a href="http://stackoverflow.com"> Cancel </a>

… possibly with some CSS to make it look like a button.

这篇关于如何创建一个重定向到URL的HTML取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆