How can I know which radio button is selected using Jquery?

4

I have two radio buttons and I want to show the value on the screen. How can you get the value with JQuery?

I can get all the elements like this:

$("form :radio")

How can I know which one is selected?

    
asked by Andresr 14.09.2017 в 02:48
source

1 answer

5

To get the value of the selected%% element of a form with id radioName :

$('input[name=radioName]:checked', '#myForm').val()

EXAMPLE:

 $('#myForm input').on('change', function() {
     alert($('input[name=radioName]:checked', '#myForm').val()); 
  });
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="myForm">
    <input type="radio" name="radioName" value="1" /> 1 <br />
    <input type="radio" name="radioName" value="2" /> 2 <br />
    <input type="radio" name="radioName" value="3" /> 3 <br />
    </form>
    
answered by 14.09.2017 / 02:53
source