jQuery and radio button groups

后端 未结 3 1899
南旧
南旧 2020-12-06 00:56

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 i

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 01:06

    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($('

    First Group name is: ' + element + '

    ')); }); });
    
    
    

    Q1:

    Male
    Female
    Other

    Q2:

    Single
    Married
    Other

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

提交回复
热议问题