如何使用jquery获取同一类的多个复选框的值

How to get the values of multiple checkboxes with the same class with jquery

本文关键字:一类 复选框 jquery 何使用 获取      更新时间:2023-09-26

我需要获取当前使用jquery检查的所有复选框的值,我想我会把它们都放在一个数组中,但我在做这件事时遇到了麻烦。有谁能给我指一下写作的方向吗。

这就是我目前使用的。我正在尝试将当前检查的所有值发送到我拥有的div。

function cityPopulate() {

        var arr44 = new Array();   
        var cityNames22 = $("input[name=city_select[]]:checked").each(function(){arr44.push(this.value);});

        $("#city_pop").append(cityNames22.val());
    }
        When I try the code above, it just gives me the value of the first checkbox I check only. Not all of the rest. 

在没有看到您的代码的情况下,我猜测您的确切模式,但在复选框中使用map()和您指定的类来创建数组应该可以工作,请尝试以下操作:

var checkboxValues = $('.myCheckbox:checked').map(function() {
    return $(this).val();
}).get();

CCD_ 2将包含一个具有选中复选框的所有值的数组。

根据您分配给复选框的类,它看起来像这样:

var values = [];
$('.checkboxclass').each(function(){
  var $this = $(this);
  if ($this.is(':checked')) {
    values.push($this.val());
  }
});