问题
How to make Enter Key Press behave like Submit in JSF. It works with InputBoxes; but not with inputSecret boxes
回答1:
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.
回答2:
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>
回答3:
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
回答4:
tested with jsf 2. put:
<h:commandButton id="hidden" style="visibility: hidden;" action="#{mybean.myaction()}" />
in your form
回答5:
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.
回答6:
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
来源:https://stackoverflow.com/questions/2419572/enter-key-press-behave-like-submit-in-jsf