JQuery radio headaches

I have 2 radio stations:

<input id="a_15_0" type="radio" value="abc" name="a_15"/>
<input id="a_15_1" type="radio" value="xyz" name="a_15"/>

      

Both are not selected. I only have the name of the radio, i.e. a_15

but not identifiers.

1) How can I get the value of the selected option of this radio? For example, if the user clicked "xyz", how can I get it? I am currently using:

var val=$('[name="a_15"]').val();

      

which always gives abc even when xyz is selected.

2) How can I get the xyz to select via javascript without knowing the id of the radio station that xyz says? Performance:

$ ('[name = "a_15"]') Val ('XYZ') ;.

Changes the value of both radios to xyz rather than fetching the value that was xyz.

Do I need to use document.myForm.a_15.value

? Will this work consistently?

0


source to share


4 answers


Have you tried using val () in conjunction with the: checked selector?

$('[name="a_15"]:checked').val();

      



As far as setting the selection based on value, you might have to do some attribute tests?

$('[name="a_15"][value="xyz"]').get(0).checked = true;

      

+1


source


1) try

var val = $('input[name=a_15]:checked').val();

      

jQuery docs on tested pseudo-class



2) the only solution found is

$('input[name=a_15][value=xyz]').get(0).checked=true

      

+2


source


There are many ways to select in jQuery ; id, class, etc. I believe this will do the job and not tested:

var val= $("input[name=a_15]:checked").val();

      

if you know the name of the form this will definitely do it

var val= jQuery('#radio_form input:radio:checked').val();

      

+1


source


you have the correct code:

for start

for editing

correct form:

$("input[name='a_15']:checked").val();

      

as you can check using the links above

0


source







All Articles