English 中文(简体)
Java 变数 项目必须选定
原标题:Java script Validation that one of f:selectItem must be selected

I want to write a javascript validation code for validating that one of f:selectItem must be selected but i m unable to access checked property of f:selectItem through java script. Below is my JSF page.

JSF 页: 1

<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html> 
 <f:view> 
    <body 
        <h:form styleClass="form" id="form1"">
            <h:selectOneRadio id="searchType" value="#{auditTrailDTO.searchType}">
            <f:selectItem itemLabel="Male" itemValue="m"/>
            <f:selectItem itemLabel="FeMale" itemValue="f"/>
        </h:selectOneRadio>
        </h:form>
    </body>
</f:view>
 </html>

<>AuditTrailDTO.java:

class AuditTrailDTO
 private string searchType;
 public String getSearchType() {
    return searchType;
 }
 public void setSearchType(String searchType) {
    this.searchType = searchType;
 }
}

www.un.org/Depts/DGACM/index_spanish.htm Edit:-

I have succeeded in accessing f:selectItem by following way.Now when no one is selected it shows alert message as expected.But it shows alert message even when second of the radio button is selected. I m calling below method on form onsubmit attribut.Any idea please?

function validateForm(){
var searchType = document.getElementsByName("form1:searchType");
var a = !(searchType[0].checked);
var b = !(searchType[1].checked);
if(a&&b){
    alert( Please select search type );
        return false;
}
 return true;
} 
最佳回答

您可尝试requiredrequiredMessage属性:

<h:selectOneRadio required="true" requiredMessage="Message... ">
...
</h:selectOneRadio>

您可浏览requiredMessage:

<h:messages> or <h:message>

问题回答

Set the

如果是,

<input type="radio" name="form1:searchType" value="Male">
<input type="radio" name="form1:searchType" value="FeMale">

那么,你可以在简便的JS DEMO上这样做。

window.onload=function() {
  document.getElementById( form1 ).onsubmit=function() {
    var isChecked = this.elements["form1:searchType"][0].checked || this.elements["form1:searchType"][1].checked;
    if (!isChecked) {
      alert( Please check gender );
      return false
    } 
  }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签