Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 like when i input the values of all the fields then only submit button get enabled.

like image 593
R. Rahul Avatar asked Jan 10 '11 14:01

R. Rahul


People also ask

How do I stop form submit on enter key press?

addEventListener("submit",function(e){e. preventDefault(); return false;}); This solution will now prevent the user from submit using the enter Key and will not reload the page, or take you to the top of the page, if your form is somewhere below.

How do I stop a form from submitting ajax?

To submit a form without Ajax, you can either disable Ajax form handling globally, or per form via the data-ajax="false" attribute. The target attribute (as in target="_blank" ) is respected on forms as well, and will default to the browser's handling of that target when the form submits.

How do you prevent form submit on Enter in JQuery?

Disallow enter key anywhere$(document). on("keydown", "form", function(event) { return event. key != "Enter"; });

How do I stop a form from submitting?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.


1 Answers

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>
like image 68
RoToRa Avatar answered Sep 28 '22 02:09

RoToRa