问题
How do you get which radio is selected within p:selectOneRadio
using javascript/jquery ?
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>
回答1:
You can use the jquery solution or choose a simple javascript solution:
document.getElementById("myFormId:mySelectId")[0].checked
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...
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>
,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>
The server side code:
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);
}
the ReuqestContext component can be found here: http://www.primefaces.org/showcase-labs/ui/requestContext.jsf (only for PF 3)
回答2:
The problem could be solved easily with jquery find() by using getJQ method of the pf widget:
<p:selectOneRadio ... widgetVar="mySelectName" .../>
we can obtain current value by calling:
PF('mySelectName').getJQ().find(':checked').val();
回答3:
If you just need the value of selected item in JS you can call your function with this.value
parameter.
Look inside PF generated HTML:
<input id="{component_id}:{index}" name="{component_id}" type="radio" value="{itemValue}"
checked="checked" onchange="reactToChangedRadio(this.value);">
...
for each selectItem
.
Hence this.value
works perfectly, no need to search for checked
:
<script>
function reactToChangeRadio(par){
alert('Value of selected item is: ' + par);
}
</script>
<p:selectOneRadio onchange="reactToChangedRadio(this.value);" >
<f:selectItem itemLabel="....." itemValue="..." />
<f:selectItem itemLabel="....." itemValue="..." />
<f:selectItem itemLabel="....." itemValue="..." />
</p:selectOneRadio>
Or you can pass this.id
into function to use it in jQuery selector.
回答4:
you have yo use the :checked pseudo selector: More info here: http://api.jquery.com/checked-selector/
$(":checked")
回答5:
All you need to do is to disable all radio buttons except the one that was checked like:
<h:selectOneRadio id="rad" ... onclick="dataTableSelectOneRadio(this)" >
<f:selectItems .... />
</h:selectOneRadio>
And here is your script:
function dataTableSelectOneRadio(radio){
var id = radio.name.substring(radio.name.lastIndexOf(':'));
var temp = radio.name.lastIndexOf(':') - 1;
var index = radio.name.substring(temp,temp+1);
var el = radio.form.elements;
for(var i = 0 ; el.length; i++)
{
if(el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
if(el[i].name.indexOf(index) != -1)
el[i].check = true;
else
el[i].check = false;
}
}
}
回答6:
There is a one-line solution to this question that also handles the case where you have multiple selectOneRadio inputs on a page.
If your form is called myForm
, and your selectOneRadio input has the id myRadio
, then the following line will get the selected value:
$('[id^=myForm\\:myRadio]:checked').val()
This works because all the internal inputs are given an id beginning with myForm:myRadio
. The line above finds all entities beginning with that string, and selects only the checked ones. Since only one radio button can be checked a a time, this gives the selected radio button. Then we can simply grab its value.
来源:https://stackoverflow.com/questions/8385591/how-to-get-the-selected-option-from-pselectoneradio-using-javascript