OnBlur导致单击“提交”按钮两次

时间:2022-09-26 09:47:46

I have a form which is doing client side js validation via ajax. Its a simple send email (contact) form with 3 fields, name, email and message. if you try clicking submit without proper validation, error msg divs become visible. Each of these 3 inputs call js functions via onblur. If I try entering the proper input then clicking submit once, it doesnt work. Click a second time and it works.

我有一个表单,通过ajax进行客户端js验证。它是一个简单的发送电子邮件(联系)表单,包含3个字段,名称,电子邮件和消息。如果您尝试在没有正确验证的情况下单击提交,则会显示错误消息div。这3个输入中的每一个都通过onblur调用js函数。如果我尝试输入正确的输入然后单击提交一次,它就不起作用。再次点击它就可以了。

Reason why is I need to unfocus from that field so that the onblur validation function processes THEN it submits. This seems like it should be a common problem. Submit calls a php script which makes an ajax call to validate the 3 fields and if correct, submits the email while displaying a "success" msg.

原因是我需要从该字段中取消焦点,以便onblur验证函数处理它提交的内容。这似乎应该是一个常见的问题。提交调用php脚本,该脚本进行ajax调用以验证3个字段,如果正确,则在显示“成功”消息时提交电子邮件。

Any idea how I should stop this double clicking submit button??

知道如何停止这个双击提交按钮?

I didnt provide code because I assume it will make this too long, but for help.. the 3 fields return false if bad and true if good, then the php script just checks for 2 of the fields if they are empty and the email field if its formatted correctly via regex. Then if so uses the mail function and then prints "success" in a div.

我没有提供代码,因为我认为它会使这个太长,但是为了帮助.. 3个字段返回false,如果不好和真如果好,那么php脚本只检查2个字段,如果它们是空的和电子邮件字段如果通过正则表达式正确格式化。然后,如果是这样使用邮件功能,然后在div中打印“成功”。

3 个解决方案

#1


0  

I think you have no alternative than to (when submit is clicked), flag that submit was clicked, and then in the validator call backs, if all is valid, check the flag, if it's set, then submit the form.

我认为除了(点击提交时),标记提交被点击,然后在验证器回调中,如果一切有效,检查标志,如果已设置,则提交表单。

document.formname.submit();

#2


0  

I just solved the problem. Onblur was definitely the issue. I changed onblur to onkeyup that way after each key is pressed it re-validates everything, therefore once I try to click on "Submit" it has already validated the input client side and it immediately runs the server side script.
Thanks for all the input everyone.

我刚解决了这个问题。 Onblur绝对是个问题。在按下每个键后,我将onblur更改为onkeyl,然后重新验证所有内容,因此一旦我尝试单击“提交”,它已经验证了输入客户端,它立即运行服务器端脚本。感谢大家的所有投入。

#3


0  

you may use

你可以用

$(document).ajaxComplete(function(){
                form.submit();     
                });

Which will wait until any ajax request completes.

这将等到任何ajax请求完成。

#1


0  

I think you have no alternative than to (when submit is clicked), flag that submit was clicked, and then in the validator call backs, if all is valid, check the flag, if it's set, then submit the form.

我认为除了(点击提交时),标记提交被点击,然后在验证器回调中,如果一切有效,检查标志,如果已设置,则提交表单。

document.formname.submit();

#2


0  

I just solved the problem. Onblur was definitely the issue. I changed onblur to onkeyup that way after each key is pressed it re-validates everything, therefore once I try to click on "Submit" it has already validated the input client side and it immediately runs the server side script.
Thanks for all the input everyone.

我刚解决了这个问题。 Onblur绝对是个问题。在按下每个键后,我将onblur更改为onkeyl,然后重新验证所有内容,因此一旦我尝试单击“提交”,它已经验证了输入客户端,它立即运行服务器端脚本。感谢大家的所有投入。

#3


0  

you may use

你可以用

$(document).ajaxComplete(function(){
                form.submit();     
                });

Which will wait until any ajax request completes.

这将等到任何ajax请求完成。