简体   繁体   English

html:在表单中使用按钮操作

[英]html: using buttons in forms witout action

I'm styling links as buttons, and have noticed that the link action is overridden by the form action. 我将链接设置为按钮样式,并注意到链接操作被表单操作覆盖。 So a cancel button actually updates. 因此取消按钮实际更新。 Is there a way to remove this behaviour easily and reliably(across all browsers) from the button, while keeping it inside the form element? 有没有办法从按钮轻松可靠地(跨所有浏览器)删除此行为,同时保持在表单元素内? Or do I need to restyle the buttons? 或者我需要重新按下按钮?


update: 更新:

<form>
 <input type='text'>
 <a href="cancel"><button>cancel</button></a>
 <input type="submit">
</form>

right now the a>button has the same action as input(submit) 现在a>buttoninput(submit)具有相同的动作input(submit)


ended up restyling and removing the button tags 最终重新定位并删除button标签

Sorry if I have the wrong end of the stick here but I am not entirely sure I understand your question as that cancel button shouldnt be submitting your form, you are however using buttons incorrectly which may be the source of your problem. 对不起,如果我在这里有错误的结束,但我不完全确定我理解您的问题,因为取消按钮不应该提交您的表单,但是您使用的按钮不正确可能是您的问题的根源。

I think if you do it the way you are doing it they wont work in IE: 我想如果你这样做你不会在IE中工作:

<input type="button" value="Cancel" onClick="window.location='http://www.yoursite.com/cancellink'" />

If you do them as above should do whatever you want in all browsers unless js is disabled - hope this helps Instead of linking to a page you could call any js function inther to clear form or whatever you want 如果您按上述方法执行它们应该在所有浏览器中执行任何操作,除非禁用js - 希望这有用而不是链接到页面您可以调用任何js函数以清除表单或任何您想要的

您需要设置按钮的类型,默认情况下,如果未设置type属性,则按钮将作为表单内的提交按钮。

<a href="cancel"><button type="button">cancel</button></a>

Because of the way links work, when you click <a href="cancel"> , it will try to navigate the browser to that resource. 由于链接的工作方式,当您单击<a href="cancel"> ,它将尝试将浏览器导航到该资源。 (eg http://yoursite.com/cancel ). (例如http://yoursite.com/cancel )。

If you want your button to do some JS action, remove the <a> . 如果您希望按钮执行某些JS操作,请删除<a> It's just redundant in this case. 在这种情况下,这只是多余的。 Style the <button> how you want it. 设置<button>样式。

If you want your button to navigate to a page, remove the <button> and style the <a> . 如果您希望按钮导航到某个页面,请删除<button>并设置<a>样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM