2010-06-26 30 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

相關問題