开发者

prevent a form from being submitted while pressing enter key [duplicate]

开发者 https://www.devze.com 2023-03-11 07:12 出处:网络
This question already has answers here: Prevent users from submitting a form by hitting Enter (36 answers)
This question already has answers here: Prevent users from submitting a form by hitting Enter (36 answers) 开发者_如何学JAVA Closed 6 years ago.

how can we prevent a form from being submitted while pressing enter key.

Actually I have a text box and on entering a value on that textbox and clicking enter the textbox2 will get focused.

By default on clicking the enter button the form will get submitted. So I can not get the out put. I wrote return false on onclick event of the submit button then it works. So I would like to know how can we prevent a form submission while pressing enter.


Without looking up the actual code, here's the basic theory behind how I'd do it:

Define an event handler on the document for a key being pressed to call a function.

If the key that triggered the event is the enter/return key, return false, otherwise return true.

EDIT: Looks like you already found the answer (as well as some code to do it), so my answer is somewhat unnecessary now.


Writing 'onKeyPress' of the form like " { var key; if(window.event) key = window.event.keyCode; else key = e.which; //firefox return (key != 13); } " will work.


what i do is add onsubmit="return false;" to the <form> tag. this will also disable the submit button however to create a working submit button use

<input type="button" value="Submit" onclick="javascript:document.form.submit();" />


This should work

<form onsubmit="return false;">
    //Form elements
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消