簡體   English   中英

如何使用jQuery在數組中插入選中復選框的值

[英]How to insert value of checked checkbox in array using jQuery

我想選擇所有已checked並具有名稱DisbursementUnitCodes輸入值

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

這段代碼給出了第一個元素的值,但是我想給出所有輸入值到列表或數組。

.val()為您提供選擇中第一個元素的值。 如果想要數組中的所有值,則應使用.map()如下所示:

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

使用.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" /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM