【问题标题】:How to store all the checked checkboxes class name in an array?如何将所有选中的复选框类名存储在数组中?
【发布时间】:2014-04-16 04:48:10
【问题描述】:

我正在尝试将所有选中的复选框类名称存储在一个数组中,以便稍后使用 ajax 函数对其进行解析。

但首先要确保所有类都正确存储在数组中,我会提醒他们,但要知道,我看到的只是第一个选中的复选框类

应该有很多,因为复选框是动态生成的。

jQuery

$(document).ready(function() {
    $('#actselect').change(function() {
      var conceptName = $( "#actselect option:selected" ).attr ( "id");
      if (conceptName == "payall"){
     var checkedB = new Array();
         checkedB.push($("input.NP:checkbox:checked").attr("class"));
     alert(checkedB);
  }

});

HTML

   a php while loop {

    <input type="checkbox"  value="None" id="itemselectNP" class="NP T<?php echo $productId; ?>" name="itemselect" />

   }

请告诉我我的代码有什么问题?我尝试了谷歌搜索中弹出的不同解决方案,但仍然没有运气。

赞赏

【问题讨论】:

    标签: jquery arrays checkbox


    【解决方案1】:

    现在使用 each() 来跟踪多个元素,它只选择一个复选框,因此使用每个复选框来存储它们的所有类

    $(document).ready(function() {
        $('#actselect').change(function() {
          var conceptName = $( "#actselect option:selected" ).attr ( "id");
          if (conceptName == "payall"){
         var checkedB = new Array();
             $("input.NP:checkbox:checked").each(function(){
               checkedB.push($(this).attr("class"));
             });
         alert(checkedB);
      }
    
    });
    

    【讨论】:

    • 藤你的答案是如此简单和可行,非常感谢,如果你也能投票支持我的问题,我们将不胜感激。 :)
    • 有人无缘无故又给我投了反对票,你能帮帮我吗?
    【解决方案2】:

    试试这个

    var $array = [];
    
    $.each($('input[type= checkbox]:checked'),function(i,val){
        $array.push('your want to push the value');
    });
    

    【讨论】:

      【解决方案3】:

      试试这个方法

          var checkedB = new Array();
          $("input :checkbox:checked").each(function (){
                 checkedB.push($(this).attr('class'));
          }) ;
          alert(checkedB);
      

      快乐编码:)

      【讨论】:

        【解决方案4】:

        <script type='text/javascript'>
              $(document).ready(function() {
        
              GetSelectedCheckbox();
            $('#btnGet').click(function(){
                GetSelectedCheckbox();
            });
            $('#btnSave').click(function() {
                    addCheckbox($('#txtName').val());
                });
            });
            function GetSelectedCheckbox()
            {
            var checkedB = new Array();
            $('input[type=checkbox]').each(function () {
                if(this.checked)
                {
                    //alert($(this).attr('class'));
                    checkedB.push($(this).attr("class"));
                }
        
             });
            alert(checkedB);
            }
            function addCheckbox(name) {
               var container = $('#checkDiv');
               var inputs = container.find('input');
               var id = inputs.length+1;
        
               $('<input />', { type: 'checkbox', id: 'cb'+id, value: name,class:'class'+id }).appendTo(container);
               $('<label />', { 'for': 'cb'+id, text: name }).appendTo(container);
            }
              </script>
        

        http://jsfiddle.net/u8sRb/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-13
          • 2014-10-04
          • 1970-01-01
          • 2016-06-14
          • 1970-01-01
          相关资源
          最近更新 更多