简体   繁体   English

使一个提交按钮响应点击回车并单击

[英]making a submit button respond to hitting enter and click

I see on many sites that the enter key will submit a form but you could also click on the button to submit. 我在许多站点上看到回车键都会提交表单,但是您也可以单击按钮提交。 Is there a trick to this? 有这个窍门吗? Or can a button have two different types of functions attached? 还是按钮可以连接两种不同类型的功能?

If you have a properly marked up form with an input element of type=submit as your submit button, the enter button should submit the form by default (when focused in an input element of type=text). 如果您有一个正确标记的表单,其输入按钮为type = submit作为输入按钮,则默认情况下,输入按钮应提交该表单(当焦点位于type = text的输入元素时)。 Clicking works as well. 单击也可以。

Any processing code would go on the form's "onsubmit" event instead of the button's "onclick" event. 任何处理代码都将在表单的“ onsubmit”事件而不是按钮的“ onclick”事件上进行。 Additionally, you can return false to cancel the form submission (for example, if form validation fails). 此外,您可以返回false以取消表单提交(例如,如果表单验证失败)。

My dear there is nothing tricky. 亲爱的,没有什么棘手的事情。 Just put the type of the button as submit and your will achieve what you are wanting. 只需将按钮的类型作为submit完成您所想要的。 See This 看这个

After UPDATE 更新后

When there is a form tag in your html page, enter button always submit the form. 当您的html页面中有一个form标签时,请输入按钮始终提交表单。 If you have made your own button having type="button" even then you can submit form onclick it. 如果您自己制作的按钮甚至具有type="button" ,则可以提交onclick表单。

<input type="button" onclick="submitform();" value="Button">

and here is your javascript function 这是你的javascript函数

function submitform()
 {
   document.forms["myform"].submit();
 }

Hence your both cases can be achieved 因此,两种情况都可以实现

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

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