views:

771

answers:

3

hi , i have a form that works perfectly fine with mozilla and IE but on google chrome there is a slight problem i am using ajax to submit my form that is there is no submit button ,in IE and Mozilla its working fine but in google chrome when i press enter the form submits and the page redirects to the main page( this is another problem page refresh manipulation in ajax) how can i stop the page from submission while pressing enter button ?

+1  A: 

Return false from the function submitting the form.

bingle
thanks , for the answer ;)
+3  A: 

Each event has its default action (a button will click, a hyperlink will take you somewhere) and in your case an ENTER on a form element will submit the form. Such actions can be prevented. Browsers differ from their implementation to prevent this default action, but tools like jQuery or MooTools help you with this.

Clear details on how to go about can be found here:

http://www.quirksmode.org/js/events_early.html#link4

The idea is either to write an onsubmit handler on the form element as such:

<form id="foo" onsubmit="doYourAjaxThing(); return false">

Or to have an event listener attached to your form by javascript and do something like this (jQuery syntax):

$("foo").submit(function(event){
 doYourAjaxThing()      
 event.preventDefault();
});

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

Martin Kool
+1  A: 
$("input").keypress(function (e) {
    if (event.keyCode == 13) {
        //Put whatever you would rather happen here........
        return false;    //this cancels the SUBMIT action
    }
});

Good luck, Patrick

Patrick Beardmore