【问题标题】:How to insert value of checked checkbox in array using jQuery如何使用jQuery在数组中插入选中复选框的值
【发布时间】:2016-07-10 07:50:39
【问题描述】:

我想选择所有输入值 checked 并且名称为 DisbursementUnitCodes

$('input:checked[name=DisbursementUnitCodes]').val()

这段代码给出了第一个元素的值,但我想把所有的输入值都给列表或数组。

【问题讨论】:

    标签: javascript jquery html arrays input


    【解决方案1】:

    使用.each() 迭代选定的元素并获取其值。

    var arr = [];
    $("input:checked[name=DisbursementUnitCodes]").each(function(){
        arr.push($(this).val());
    });
    console.log(arr);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" name="DisbursementUnitCodes" value ="A" checked />
    <input type="checkbox" name="DisbursementUnitCodes" value ="B" />
    <input type="checkbox" name="DisbursementUnitCodes" value ="C" checked />
    <input type="checkbox" name="other" value ="D" checked />
    <input type="checkbox" name="other" value ="E" />

    【讨论】:

      【解决方案2】:

      .val() 为您提供选择中第一个元素的值。如果你想要数组中的所有值,你应该像这样使用.map()

      var values = $('input:checked[name=DisbursementUnitCodes]').map(function() {
          return $(this).val();
      }).get();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2020-10-10
        相关资源
        最近更新 更多