当有两个提交按钮时,使用回车键提交表单提交错误 [英] Wrong form submission using Enter Key when there are two submit buttons

查看:61
本文介绍了当有两个提交按钮时,使用回车键提交表单提交错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个提交按钮的表单 - 一个用于取消,另一个用于将表单值保存到数据库。当按Enter键时,取消按钮提交表单而不是提交表单的保存按钮。如何按下Enter键时保存按钮提交表单?

谢谢,

解决方案

您的表单不应该有两个提交按钮。编辑:我要更新这个答案来处理几个带来的问题。



我在HTML规范中没有看到任何东西(即 DTD )不允许一个表单中的两个提交按钮,但OP提到的确切问题可能会发生。在他或她的情况下,解决方案是制作类型按钮的取消按钮并添加以下JavaScript:

 < input type =buttonvalue =取消onclick =window.location.href ='nextpage.html';/> 

这里,可以用 nextpage.html 与适当的URL。或者, this.form.action 可以是新的位置,如果重定向是针对表单的操作。



如果OP想要安全并避免使用JavaScript,这只能是一个(可能是风格的)超链接。


I have a form with two submit buttons - one for Cancel and the other is for Saving form values to the DB . When Enter key is pressed, the Cancel button submits the form instead of the Save button submitting the form. How can I make the Save button submit the form when Enter key is pressed?

Thanks,

解决方案

Your form should not have two submit buttons. Have the Save button be of type submit, and the Cancel button be of type button.

EDIT: I'm going to update this answer to handle several issues that were brought up.

Nothing I've seen in the HTML specification (i.e. the DTD) disallows two submit buttons in one form, but the exact issue the OP mentioned can occur. In his or her case, the solution is to make the Cancel button of type button and add in the following JavaScript:

<input type="button" value="Cancel" onclick="window.location.href='nextpage.html';"/>

Here, one would replace nextpage.html with the appropriate URL. Or, this.form.action can be the new location if the redirection is to the action of the form.

If the OP wants to be safe and avoid JavaScript, this could only be a (perhaps styled) hyperlink.

这篇关于当有两个提交按钮时,使用回车键提交表单提交错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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