Disable form submission on pressing enter because i am using Ajax functionality
I am using ajax functionality. When i am selecting any suggestion using enter key, the form get submitted automatically because the submit button is auto-focused. Is there any way to select ajax suggestion using enter key as well submit the form using enter key. Something lik开发者_运维知识库e when i input the values of all the fields then only submit button get enabled.
How are you calling your Ajax function? It should be inside the submit
event handler of the form. And then if you have the event handler return false
it won't submit normally:
<form onsubmit="doAjaxSubmit(); return false;">
...
</form>
You can detect which key is pressed using the .keypress()
method. From this you should be able to return false;
for form submit and do your ajax stuff on the back of it in an if
statement:
http://api.jquery.com/keypress/
You can have your form's onSubmit
return true
if all the fields have values, or false
if they don't.
精彩评论