I have bound a JavaScript function to the submit button of a form, causing the form to be submitted via an xhrPost request (using Dojo). However, when the user hits "Enter" in Safari, the form is submitted the usual way.
我已将JavaScript函数绑定到表单的提交按钮,导致表单通过xhrPost请求(使用Dojo)提交。但是,当用户在Safari中点击“Enter”时,表单以通常的方式提交。
Is there any way to prevent Safari from submitting the form when the Enter key is pressed? Can I somehow bind my JavaScript function to the enter key instead?
当按下Enter键时,有没有办法阻止Safari提交表单?我可以以某种方式将我的JavaScript函数绑定到enter键吗?
Thanks a lot in advance!
非常感谢提前!
--Andreas
3 个解决方案
#1
Are you returning the false
value from from the onsubmit
handler? That's all that should be required to prevent normal form submission (provided Javascript is enabled).
您是否从onsubmit处理程序返回false值?这就是防止正常表单提交所需的全部内容(如果启用了Javascript)。
#2
This is probably happening due to an error in the submission handler code - Safari will submit the form as usual if there is an exception there. Check to see if the Safari console is showing any errors.
这可能是由于提交处理程序代码中的错误而发生的 - 如果存在异常,Safari将照常提交表单。检查Safari控制台是否显示任何错误。
I also had a similar issue once when the <input>
elements were outside of the <form>
tag. Double check that, too, perhaps.
当元素在
#3
How I solved this problem was to disable the 'Enter' key. Not very elegant, and still looking for a better approach.
我如何解决这个问题是禁用'Enter'键。不是很优雅,仍然在寻找更好的方法。
$('form').keypress(function (e){ if((e.which==13){ return false; }});
$('form')。keypress(function(e){if((e.which == 13){return false;}});
#1
Are you returning the false
value from from the onsubmit
handler? That's all that should be required to prevent normal form submission (provided Javascript is enabled).
您是否从onsubmit处理程序返回false值?这就是防止正常表单提交所需的全部内容(如果启用了Javascript)。
#2
This is probably happening due to an error in the submission handler code - Safari will submit the form as usual if there is an exception there. Check to see if the Safari console is showing any errors.
这可能是由于提交处理程序代码中的错误而发生的 - 如果存在异常,Safari将照常提交表单。检查Safari控制台是否显示任何错误。
I also had a similar issue once when the <input>
elements were outside of the <form>
tag. Double check that, too, perhaps.
当元素在
#3
How I solved this problem was to disable the 'Enter' key. Not very elegant, and still looking for a better approach.
我如何解决这个问题是禁用'Enter'键。不是很优雅,仍然在寻找更好的方法。
$('form').keypress(function (e){ if((e.which==13){ return false; }});
$('form')。keypress(function(e){if((e.which == 13){return false;}});