2010-06-26 71 views
1

我有个/组单选按钮,和一个命令是否可以在jsf中做到这一点?

两种情况

1)当按钮之一被点击+点击命令按钮,我应显示一些其它部分(比如一个DIV )并停止执行。装置,该命令按钮不应形式提交给服务器

1)当按钮的NONE被点击+点击命令按钮,我应该提交表单到服务器,并显示一个faces消息,他没有选择任何东西。

我不想做这个警报作为JavaScript popup.Suggestions是受欢迎的。

+0

_“当单击其中一个按钮时,_是指您选择单选按钮还是其他? – McDowell 2010-06-26 11:52:33

+0

是的。你说得对。 – gekrish 2010-06-26 15:05:06

回答

2

是的,这可能需要JavaScript的帮助。最简单的方法是将div div元素的ID作为单选按钮值,然后遍历所有单选按钮,并根据按钮的checked状态显示/隐藏元素。最后,当任何按钮被选中时,JS函数应该返回false,这样按钮的默认动作将不会被调用,并且表单将不会被提交给服务器。

这里有一个开球例如:

<h:form id="form"> 
    <h:selectOneRadio id="radio"> 
     <f:selectItem itemValue="section1" itemLabel="Show section1" /> 
     <f:selectItem itemValue="section2" itemLabel="Show section2" /> 
     <f:selectItem itemValue="section3" itemLabel="Show section3" /> 
    </h:selectOneRadio> 
    <h:commandButton id="button" value="show section" 
     action="#{bean.submit}" onclick="return showSection(this.form)" /> 
    <h:messages /> 
</h:form> 
<h:panelGroup layout="block" id="section1" class="section">section1</h:panelGroup> 
<h:panelGroup layout="block" id="section2" class="section">section2</h:panelGroup> 
<h:panelGroup layout="block" id="section3" class="section">section3</h:panelGroup> 

这个CSS

.section { 
    display: none; 
} 

和这个JS

function showSection(form) { 
    var radiobuttons = form['form:radio']; 
    var selected = false; 
    for (var i = 0; i < radiobuttons.length; i++) { 
     var radiobutton = radiobuttons[i]; 
     var section = document.getElementById(radiobutton.value); 
     if (radiobutton.checked) { 
      section.style.display = 'block'; 
      selected = true; 
     } else { 
      section.style.display = 'none'; 
     } 
    } 
    return !selected; // Submits to server if none is selected. 
} 

和JSF豆(当然,我们认为这一行动不仅会如果没有被选中,则调用)。

public void submit() { 
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please select!")); 
} 
+0

你是一个美丽的心灵。 – gekrish 2010-06-26 15:15:29