I have 2 radio butto开发者_开发问答ns on my page:
<input id="rb1" type="radio" name="rmode" />
<input id="rb2" type="radio" name="rmode" />
How can I figure out which one was selected using jquery?
...
<input type="radio" name="sample" value="1" />
<input type="radio" name="sample" value="2" checked="checked" />
<input type="radio" name="sample" value="3" />
...
//javascript code
<script type="text/javascript">
<!--
// displays the selected radio button in an alert box
alert($('input[name=sample]:checked').val())
-->
</script>
You can use the :radio selector and also combine that with :checked to get the checked radio element.
This should work for you:
$("input[name=rmode]:radio:checked");
Example on jsfiddle
This should return which radio buttons are currently checked:
var id = $("input[@name=rmode]:checked").attr('id');
More information here. Hope that helps!
精彩评论