获取单选按钮组的值

发布于 2024-09-14 03:52:29 字数 705 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-09-21 03:52:29

您的引号只需包含 attribute-equals 选择器 的值部分,[attr='val'],如下所示:

$('a#check_var').click(function() {
  alert($("input:radio[name='r']:checked").val()+ ' '+
        $("input:radio[name='s']:checked").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());
});​

You can see the working version here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文