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();             
        }
    });

3 Answers 3

4

Try

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

What is the difference between 2 vs. 3 "=" signs?
Checks for equality with / without type coercion, respectively. See this question: stackoverflow.com/questions/523643/… (In your case, either should work)
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

Comments

0

i think you want preventDefault()!

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

try putting this instead of setting it to 0

e.preventDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.