Monday 27 February 2017

javascript - How can I know which radio button is selected via jQuery?



I have two radio buttons and want to post the value of the selected one.
How can I get the value with jQuery?




I can get all of them like this:



$("form :radio")


How do I know which one is selected?


Answer



To get the value of the selected radioName item of a form with id myForm:




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


Here's an example:





$('#myForm input').on('change', function() {
alert($('input[name=radioName]:checked', '#myForm').val());
});




1
2
3




No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...