Primefaces commandButton submitting on enter
I have two 开发者_Python百科commandButtons and when I hit enter the first one submits. I really only want to submit the second button if the user hits enter. Any ideas?
I realize that the question is old, but maybe it is still interessing for someone to see a different solution.
You can use the Primefaces defaultCommand and adding an id to your prefered Button to define what happens when the Enter key is pressed. That's all.
<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" id="btn_to_enter" action="#{bean.action2}"/>
<p:defaultCommand target="btn_to_enter" />
You have to swap the position of those two buttons thats all.
Your current code should be.
<p:commandButton value="button1" action="#{bean.action1}"/>
<p:commandButton value="button2" action="#{bean.action2}"/>
By default the button1 action will be triggered. You can present the user an alternative view, by adding style="float:right" to the button1.
<p:commandButton value="button1" style="float: right" action="#{bean.action2}"/>
<p:commandButton value="button2" action="#{bean.action1}"/>
Using the above the button1 will appear after the button2, and perform the action of button2, whenever the Enter is pressed.
精彩评论