获取单选按钮组的值
我正在尝试使用 jQuery 语法获取两个单选按钮组的值,如下所示。运行下面的代码时,我两次获取从第一个单选按钮组中选择的值,而不是获取每个单独组的值。
我在这里做明显错误的事情吗?感谢您的帮助:)
<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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的引号只需包含 attribute-equals 选择器 的值部分,
[attr='val']
,如下所示:您可以在此处查看工作版本。
Your quotes only need to surround the value part of the attribute-equals selector,
[attr='val']
, like this:You can see the working version here.