开发者

how to figure out which radio button was selected?

开发者 https://www.devze.com 2023-02-08 03:01 出处:网络
I have 2 radio butto开发者_开发问答ns on my page: <input id=\"rb1\" type=\"radio\" name=\"rmode\" />

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消