您可以使用 jquery 解决方案或选择简单的 javascript 解决方案:
document.getElementById("myFormId:mySelectId")[0].checked
请参阅 CodeRanch 的帖子:http: //www.coderanch.com/t/210871/JSF/java/selectOneRadio-javascript-value
更新:我必须承认我在一个部门,对此我很抱歉,但昨天我没有太多时间......
我必须说我无法以老式的 javascript 方式获取无线电值:
<script type="text/javascript">
/* <![CDATA[ */
function reactToChangedRadio(){
alert("I'm in!");
var myval;
for(i=0;i<3;i++){
if(document.forms['myFormId']['myFormId:myRadio'][i].checked == true ){
myval = document.forms['myFormId']['myFormId:myRadio'].text/value;
}
}
alert( "val = " + myval );
}
/* ]]> */
</script>
另一方面,这个硬编码的解决方案有效:
<script type="text/javascript">
/* <![CDATA[ */
function reactToChangedRadio(){
alert("I'm in");
var myval;
if(document.forms['myFormId']['myFormId:myRadio'][0].checked == true ){
myval = "first button";
}else if(document.forms['myFormId']['myFormId:myRadio'][1].checked == true ){
myval = "second button";
}else if(document.forms['myFormId']['myFormId:myRadio'][2].checked == true ){
myval = "third button";
}
alert( "val = " + myval );
}
/* ]]> */
</script>
,但当然,由于 Primefaces 的强大,有一个服务器端解决方案(使用 ReuqestContext 组件):
<h:form id="myFormId">
<p:selectOneRadio id="myRadio" value="#{handleFiles.radioVal}" >
<p:ajax event="change" oncomplete="handleComplete(xhr, status, args)" listener="#{handleFiles.testMethod}" />
<f:selectItem itemLabel="1" itemValue=" first" />
<f:selectItem itemLabel="2" itemValue=" second" />
<f:selectItem itemLabel="3" itemValue=" third" />
</p:selectOneRadio>
</h:form>
<script type="text/javascript">
function handleComplete(xhr, status, args) {
alert("Selected Radio Value" + args.myRadVal);
}
</script>
服务器端代码:
private String radioVal;
public String getRadioVal() {
return radioVal;
}
public void setRadioVal(String radioVal) {
this.radioVal = radioVal;
}
public void test(){
RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("myRadVal", radioVal);
System.out.println("radioVal: "+radioVal);
}
ReuqestContext 组件可以在这里找到:http: //www.primefaces.org/showcase-labs/ui/requestContext.jsf(仅适用于 PF 3)