如何使用JavaScript从p:selectOneRadio获取所选选项 [英] How to get the selected option from p:selectOneRadio using javascript
问题描述
如何获取使用JavaScript/jquery在p:selectOneRadio
中选择了哪个无线电?
How do you get which radio is selected within p:selectOneRadio
using javascript/jquery ?
由于p:selectOneRadio
不使用任何单选标记,所以我不知道如何使用CSS选择器获取选中的选项.
Since the p:selectOneRadio
uses no radio tags I have no idea how to get the checked option using CSS selectors.
<p:selectOneRadio onchange="reactToChangedRadio()" >
<f:selectItem itemLabel="....." itemValue="..." />
<f:selectItem itemLabel="....." itemValue="..." />
<f:selectItem itemLabel="....." itemValue="..." />
</p:selectOneRadio>
推荐答案
您可以使用jquery解决方案或选择简单的javascript解决方案:
You can use the jquery solution or choose a simple javascript solution:
document.getElementById("myFormId:mySelectId")[0].checked
请参阅CodeRanch中的帖子: http://www. coderanch.com/t/210871/JSF/java/selectOneRadio-javascript-value
See the post from CodeRanch: http://www.coderanch.com/t/210871/JSF/java/selectOneRadio-javascript-value
更新: 我必须承认我在一个部门,对此我感到抱歉,但是昨天我没有太多时间...
UPDATE: I must admit that I'm in a dept, and I'm sorry for that but yesterday I haven't had much time...
我必须说,我无法以老式的javascript方式获取无线电值:
I must say that I haven't been able to get the radio value in the old fashioned javascript way:
<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>
另一方面,此硬编码解决方案有效:
On the other hand, this hard-coded solution works:
<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组件):
,but of course, because of Primefaces power, there is a server side solution(using ReuqestContext component):
<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)
the ReuqestContext component can be found here: http://www.primefaces.org/showcase-labs/ui/requestContext.jsf (only for PF 3)
这篇关于如何使用JavaScript从p:selectOneRadio获取所选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!