开发者

Enter Key Press behave like Submit in JSF

How to make 开发者_高级运维Enter Key Press behave like Submit in JSF. It works with InputBoxes; but not with inputSecret boxes


I haven't seen this issue before. The chance is little that this behaviour is browser specific. Try in different kinds of browsers to exclude the one and other (IE6/7/8, FF, Safari, Chrome, etc). The chance is bigger that this is caused by a (poor) Javascript key event listener which incorrectly suppresses the enter key (for example a JS password validator which checks the entered characters).

If still in vain, just add the following onkeypress to the h:form:

<h:form onkeypress="if (event.keyCode == 13) this.submit();">

You need to take textareas into account however. If you have them, then you need to copy all onkeypress events over the h:inputXXX elements expect of textareas yourself.


If you want to press ENTER key instead of submit in any form, what we have to do here is add defaultcommand attribute in af:form tag and give id of the submit button as value. Sample code snippet for this is

<af:form id="f1" defaultCommand="cb1">
<af:outputText value="User Name" id="usename"/>
          <af:inputText value="#{BackingBean.userName}" id="uname" />
          <af:outputText value="Password" id="pword"/>
          <af:inputText value="#{BackingBean.password}" id="paword" secret="true"/>
          <af:commandButton text="Submit" action="#{BackingBean.method}" id="cb1" />
</af:form> 


I made it work by placing an additional inputBox and hiding it using javascript. Let me know if you have any other suggestions Thanks baluC for pointing me in the right direction Jerry


tested with jsf 2. put:

<h:commandButton id="hidden" style="visibility: hidden;" action="#{mybean.myaction()}" />

in your form


From ComputerPilot's answer I came to know that it's bug in IE which wouldn't make the parent form to submit if there's only one input element. To overcome this problem I added one more input Box with attribute style="display:none" and it worked fine.


There is an old specification that pops into my mind with this one. If you have a form that contains just one input field, the behaviour of submitting on the enter-key doesn't work in some versions of Internet Explorer. The easiest fix is to make sure you have more than one input field.

Other reasons for this problem include...

  • Not having an input of type submit
  • Having an input of type submit, but it isn't visible on the page (hidden or positioned off-page)

This behaviour is very specific to this browser.

MS Bug Report Here: connect.microsoft.com: submit button value not posted with form submission

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜