开发者

Form submission on pressing enter button

开发者 https://www.devze.com 2023-02-03 15:23 出处:网络
I am handling ajax suggestions using keyboard and mouse, it is capturing every keyevent except enter key(13). What I want is to get the "selected suggestion value" into the text box. For thi

I am handling ajax suggestions using keyboard and mouse, it is capturing every keyevent except enter key(13). What I want is to get the "selected suggestion value" into the text box. For this I am handling keyevent = 13. Now the problem is when I am pressing enter key, my form get 开发者_运维问答submitted instead of going into the "if block" where I am checking (keyevent = 13).

I am using struts <html:submit> tag to submit my form. I guess, the browser automatically set the focus into first <html:submit> tag that comes in its place. How to defocus this? I tried setting focus at other fields but trick doesn't work.

The other way is, I can use simple <html:button> and can get the things working, but the system already using <html:submit>. So, getting approval and modification is quite hectic.

Code for submit button:

<html:submit styleClass="btn" property="method.saveVisaRequestForRMG" onclick="bCancel=false" styleId="submitBtn">

and code for event handling:

// Handle ENTER key 
case 13: handleSelectedItem(obj, container, cur);
     ev.cancelBubble = true;
     break;

How to come out of this problem? Please suggest me.


If you use jquery there is a simple way to handle enter press events:

  $(window).keypress(function(e) {
    if(e.keyCode == 13) {
      e.preventDefault();
      alert('Enter!');
    }
  });

After you prevented the default event you can do whatever you want for example posting the data into the server, saying hello or whatever :)


Try to return false; to cancel the event handling of the submit?


Do you have something like:

onsubmit="return formValidator()"
0

精彩评论

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