jQuery 和单选按钮组

发布于 2024-07-27 21:12:16 字数 125 浏览 4 评论 0原文

在 jQuery 中,我想选择所有未选中按钮的单选按钮组。

或者,有没有一种方法可以选择所有单选按钮组并迭代这些组?

我动态地将 N 个单选按钮组添加到页面,并且事先不知道单选按钮组的名称是什么。

In jQuery, I'd like to select all groups of radio buttons where there are no buttons checked.

Or, is there a way in which I can select all radio button groups and iterate through the groups?

I'm dynamically adding N radio button groups to a page and will not know, before hand, what the names of the radio button groups will be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

紅太極 2024-08-03 21:12:16

查找所有单选按钮组:

var radio_groups = {}
$(":radio").each(function(){
    radio_groups[this.name] = true;
})

查找哪个单选按钮组已选中单选框,哪些未选中:

for(group in radio_groups){
    if_checked = !!$(":radio[name='"+group+"']:checked").length
    alert(group+(if_checked?' has checked radios':' does not have checked radios'))
}

To find all radio groups:

var radio_groups = {}
$(":radio").each(function(){
    radio_groups[this.name] = true;
})

to find which radio group has checked radio boxes and which hasn't:

for(group in radio_groups){
    if_checked = !!$(":radio[name='"+group+"']:checked").length
    alert(group+(if_checked?' has checked radios':' does not have checked radios'))
}
记忆之渊 2024-08-03 21:12:16

支持多组和预检查。

$('input').click(function() {
    var $t = $(event.target);
    if ($t.hasClass('checked')) $t.removeAttr('checked');
    else $('input[type="radio"][name="' + $t.prop('name') + '"]').not($t).removeClass('checked');
    $t.toggleClass('checked');
}).filter(':checked').addClass('checked');​

```

证明: http://jsfiddle.net/abrkn/PGW2Z/

Support for multiple groups and pre-checked.

$('input').click(function() {
    var $t = $(event.target);
    if ($t.hasClass('checked')) $t.removeAttr('checked');
    else $('input[type="radio"][name="' + $t.prop('name') + '"]').not($t).removeClass('checked');
    $t.toggleClass('checked');
}).filter(':checked').addClass('checked');​

```

Proof: http://jsfiddle.net/abrkn/PGW2Z/

故事与诗 2024-08-03 21:12:16

要按组名称选择所有无线电并仅获取不同名称的列表:

    function selectRadioByGroupName() {
        return $.unique($('input:radio').map(function(index, element) {
            return this.name;
        }));
    }

示例片段:

function selectRadioByGroupName() {
  return $.unique($('input:radio').map(function(index, element) {
    return this.name;
  }));
}



$(function () {
  selectRadioByGroupName().each(function(index, element) {
    $('body').append($('<p>First Group name is: ' + element + '</p>'));
  });
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<form action="">
    <p>
        Q1:
    </p>
    <input type="radio" name="gender" value="male"> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other
    <br />
    <p>
        Q2:
    </p>
    <input type="radio" name="status" value="male"> Single<br>
    <input type="radio" name="status" value="female"> Married<br>
    <input type="radio" name="status" value="other"> Other
    <br />
    <input type="button" name="submit_id" value="Submit" onclick="submitAnswers()">
</form>

获得不同的组名称后,可以循环使用它们。

To select all radio by group name and obtain only the list of different names:

    function selectRadioByGroupName() {
        return $.unique($('input:radio').map(function(index, element) {
            return this.name;
        }));
    }

An example snippet:

function selectRadioByGroupName() {
  return $.unique($('input:radio').map(function(index, element) {
    return this.name;
  }));
}



$(function () {
  selectRadioByGroupName().each(function(index, element) {
    $('body').append($('<p>First Group name is: ' + element + '</p>'));
  });
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<form action="">
    <p>
        Q1:
    </p>
    <input type="radio" name="gender" value="male"> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other
    <br />
    <p>
        Q2:
    </p>
    <input type="radio" name="status" value="male"> Single<br>
    <input type="radio" name="status" value="female"> Married<br>
    <input type="radio" name="status" value="other"> Other
    <br />
    <input type="button" name="submit_id" value="Submit" onclick="submitAnswers()">
</form>

After getting the different group names it's possible to cycle on them.

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