简体   繁体   English

即使没有提交按钮,在chrome中点击任何文本框的输入都会触发表单提交。

[英]Hitting enter in any textbox in chrome triggers the form submit, even when there is no submit button

I am building a site which uses jQUery validation plugin and want things validated before submitting the form. 我正在建立一个使用jQUery验证插件的站点,并希望在提交表单之前先对事物进行验证。 My code looks like follows 我的代码如下

<form>
  <input type="button" value="Submit the Form" onclick="validateAndSubmit()" />
</form>
<script language="javascript">

   function validateAndSubmit(){
   //do some validation and then submit 
 }

</script>

In Firefox, this works perfectly. 在Firefox中,这非常有效。 In Chrome, when I hit enter anywhere in the page, the form submit is triggered and validation doesn't work either. 在Chrome浏览器中,当我在页面上的任意位置点击Enter时,都会触发表单提交并且验证也不起作用。 Is there something to avoid this ? 有什么可以避免的吗? Shouldn't the browser not submit a form when we hit an enter if there is no submit button? 如果没有提交按钮,当我们按下Enter键时,浏览器是否应该不提交表单?

use this syntax: 使用以下语法:

<form onsubmit="return validateAndSubmit()">
...

if you need to catch the return-key maybe you can handle it by binding an keydown event to the input and perform some action on keyCode #13 如果您需要捕获返回键,也许可以通过将keydown事件绑定到输入并对keyCode#13执行一些操作来处理它

Try this method: 试试这个方法:

<form onsubmit="return validateAndSubmit(this);">
  <input type="submit" value="Submit the Form"/>
</form>
<script language="javascript">

   function validateAndSubmit(form_obj){
     if(some_variable == 'correct_value') {
        form_obj.submit();
        return true;
     } else {
        alert('Wrong value');
        return false;
     }
   //do some validation and then submit 
 }

</script>

I'm not sure if there's a standard regarding this or not. 我不确定是否有与此相关的标准。

Regardless, you can save yourself the trouble altogether by simply adopting a stronger strategy: implement the validation as an onsubmit action on the form, rather than an onclick action for the button. 无论如何,您只需采取一种更强大的策略就可以完全避免麻烦:将验证实现为对表单的onsubmit操作,而不是对按钮的onclick操作。 I almost never use buttons in forms; 我几乎从不使用表格中的按钮。 having to do so for yours would only throw me off, and that's not good for users. 不必为您自己做,只会让我失望,这对用户不利。

So anyway. 所以无论如何。 Form onsubmit is the way to go. 表单onsubmit是必经之路。 And I'd appreciate it if you used unobtrusive Javascript instead of the HTML attributes :) 如果您使用兼容的Javascript而不是HTML属性,我将不胜感激:)

暂无
暂无

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

相关问题 单击输入并按下提交按钮提交表单数据 - Submit form data both on clicking enter and hitting the submit button Chrome需要两次按“ Enter”键才能提交我的表单 - Chrome requires hitting 'Enter' twice to submit my form 点击浏览器后退按钮时提交表单 - submit a form when hitting the browser back button Chrome:在浏览器的文本框中按Enter <form> 提交页面而无需在该表单上调用onClick的提交按钮 - Chrome: On pressing Enter on a Textbox of a <form> Submits page without calling onClick of submit button on that form 使用jQuery点击Enter时提交HTML表单 - Submit html form when hitting enter using jQuery jQuery表单提交而无需单击提交按钮 - jquery form submit without hitting submit button 按下Enter键后,隐藏的提交按钮将不会以chrome提交 - Hidden submit button wont submit in chrome when enter is pressed 当提交按钮被隐藏时,通过按回车来提交表单; 在 Firefox 中工作,在 Chrome 中什么都不做 - Submitting a form by hitting return when the submit button is hidden; works in Firefox, does nothing in Chrome 表单 onSubmit={handleSubmit} 通过 type=“submit” 或未指定或在文本框上输入后的任何按钮触发 - Form onSubmit={handleSubmit} is triggered through any button which has type=“submit” or not specified or after enter on textbox 单击输入并从Bootstrap模式窗口点击提交按钮提交表单数据 - Submit form data both on clicking enter and hitting the submit button from a Bootstrap modal window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM