获取单选按钮组的值 [英] Get Value of Radio button group
本文介绍了获取单选按钮组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用下面给出的jQuery语法获取两个单选按钮组的值.运行下面的代码时,我两次从第一个单选按钮组中选择了一个值,而不是每个单独组的值.
I'm trying to get the value of two radio button groups using the jQuery syntax as given below. When the code below is run I get the value selected from the first radio button group twice instead of getting the value of each individual group.
我在这里做错了什么吗?感谢您的帮助:)
Am I doing something obviously wrong here? Thanks for any help :)
<a href='#' id='check_var'>Check values</a><br/><br/>
<script>
$('a#check_var').click(function() {
alert($("input:radio['name=r']:checked").val()+ ' ' +
$("input:radio['name=s']:checked").val());
});
</script>
Group 1<br/>
<input type="radio" name="r" value="radio1"/> radio1
<input type="radio" name="r" value="radio2"/> radio2
<br/><br/>
Group 2<br/>
<input type="radio" name="s" value="radio3"/> radio3
<input type="radio" name="s" value="radio4"/> radio4
推荐答案
您的引号仅需包含属性等于选择器,[attr='val']
,如下所示:
Your quotes only need to surround the value part of the attribute-equals selector, [attr='val']
, like this:
$('a#check_var').click(function() {
alert($("input:radio[name='r']:checked").val()+ ' '+
$("input:radio[name='s']:checked").val());
});
这篇关于获取单选按钮组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文