1

I have the following code. When the user hits [ENTER] (keycode = 13), the form is being resubmitted. I'd like to prevent a postback in this condition. When I set the keycode equal to 0, it does nothing. The page postback is still occuring. How can I prevent the post postback?

    jQuery(this.Elements.TxtClientId).keypress(function(e) {
        if (e.keyCode == 13) {
            e.keyCode = 0;
            alert("keycode: " + e.keyCode);
            thisTemp._internalClientId = jQuery(thisTemp.Elements.TxtClientId).val();
            jQuery(thisTemp.Elements.DDLClientName).val(jQuery(thisTemp.Elements.TxtClientId).val());
            thisTemp.UpdateTestDropdown();             
        }
    });
casperOne
  • 73,706
  • 19
  • 184
  • 253
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245

3 Answers3

4

Try

 if(e.keyCode === 13){
       e.preventDefault();
       // remainder of your code
   }

per the question: Prevent a button to submit on enter key

Community
  • 1
  • 1
Sean O
  • 2,276
  • 1
  • 21
  • 24
  • What is the difference between 2 vs. 3 "=" signs? – JustBeingHelpful Jun 27 '11 at 19:14
  • Checks for equality with / without type coercion, respectively. See this question: http://stackoverflow.com/questions/523643/difference-between-and-in-javascript (In your case, either should work) – Sean O Jun 27 '11 at 19:20
1

You need to prevent the default handler of the event. You are using jQuery so you can use the built in preventDefault in jQuery

Ethan Cabiac
  • 4,943
  • 20
  • 36
0

i think you want preventDefault()!

http://api.jquery.com/event.preventDefault/

try putting this instead of setting it to 0

e.preventDefault();
Patricia
  • 7,752
  • 4
  • 37
  • 70