开发者

Chrome: On pressing Enter on a Textbox of a <form> Submits page without calling onClick of submit button on that form

I have a scenario in which I have 2 Submit Buttons in a form for going back and forward.

Both the buttons have different JavaS开发者_开发百科cript associated with it. It is working great if we press these buttons to navigate.

But when in Google Chrome, when someone press enter on any of the text box within the Form it does not calls the onClick of the button.

Is there any work around for that.

Thanks


You'll need to hook the keypress event and look for key code 13, unfortunately. Not all browsers (in fact, virtually none I think) will trigger the click event on a submit button if you didn't actually click the button. They will trigger the submit event on the form.

So the pattern becomes: Put the validation (et. al.) you want to run when they click the default submit button in a function, and call that function from the keypress event if the key code is 13 and there are no modifier keys down, and from the click of that button.

When dealing with keyboard events, I find this page invaluable.


alternatively, you can use the onsubmit to do what you wanted to with the onclick of the submit.

sounds like you want to simply have this return false so that nothing happens, and they must use one of your buttons.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜