Prevent default action
This page open in newwindow. I have one text box and two button(h:commandbutton and a4j:commandButton).
I move(set) the cursor focus into textfield.
My problem is, i hit enter button from textfield, then automattically called h:comandButton action and close the new window. How to avoid this.
But, I need , when i hit enter button, move the 开发者_StackOverflow中文版cursor focus to a4j:commandButton
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function a4jButtonClick()
{
alert("A4J Command Buton clicked...");
}
</script>
</head>
<body>
<h:form id="sampleForm">
<rich:panel id="samplePanel">
<h:outputText value="User Name : "/>
<h:inputText/>
<h:commandButton value="Closing-CommandButton"
onclick="javascript:window.close();return false;"/>
<a4j:commandButton value="A4JCommandButton" onclick="a4jButtonClick()"/>
</rich:panel>
</h:form>
</body>
</html>
</f:view>
If i hit enter button from textfiled, i want to call the script alert("A4J Command Buton clicked...");
Help me. Thank in advance.
Try to add return false in onclick handler:
<h:commandButton value="HCommandButton" onclick="hButtonClick();return false;"/>
<a4j:commandButton value="A4JCommandButton" onclick="a4jButtonClick();return false;"/>
But then your form will not be submitted when you press buttons.
When you hit Enter/Return on the keyboard the first button in the markup is activated.
So you need to change the order of your button in the markup to this:
<a4j:commandButton value="A4JCommandButton" onclick="a4jButtonClick()"/>
<h:commandButton value="Closing-CommandButton" onclick="javascript:window.close();return false;"/>
Bear in mind that you can still style them to be in the original order so the design doesn't change.
精彩评论