How to get the selected value of a list item in a radio button list in jquery

I have a radio RadioButtonList

that has 3 radio buttons, I want to get a boolean (true or false) when any of the radio buttons is selected.

<asp:RadioButtonList runat="server" ID = "test" ClientIDMode="Static">
    <asp:ListItem Text="male" Value="1"  />
    <asp:ListItem Text="male" Value="2"  />
    <asp:ListItem Text="male" Value="3"  />
</asp:RadioButtonList>

      

For example: - I selected the first radio button ie [0]

var radio = $("#test");
var value = $(radio).find("[type=radio]")[0].checked;

      

But it didn't work.

+3


source to share


3 answers


I would solve your problem like this:

$("#test").find('input[type=radio]').change(function() {
     var value = $("#test").find('input[type=radio]:checked').val();
     alert(value);
})

      

This way you will notice when changes happen to your switches and will display a validation warning.



Here is a fiddle: Fiddle Works as you described. You need to apply common names to your inputs. You can do this via:

 <asp:RadioButton GroupName="String" /> 

      

Updated script with your code names +.

+1


source


Try



$("#test").find('input[type=radio]').click(function() {
      if($(this).is(':checked')){
          alert($(this).val())
      }
});

      

+3


source


You say you want to get the boolean value (true / false) of the radio button on click. In fact, you can only set a switch, which then disables the other. Hence, you can only return true when the radio button is clicked.

I prefer redistribution of names when accessing DOM elements. So I would do it like this:

$('input[type=radio]', '#test').click(function() {
        return true;
});

      

If you need a link to a specific button, you can return it like:

return $(this).val();

      

0


source







All Articles