2

I have some mark up like:

<input type="checkbox" class="fieldapprovalcheckbox" data-invoicelineid=3 />
<input type="checkbox" class="fieldapprovalcheckbox" data-invoicelineid=4 />
<input type="checkbox" class="fieldapprovalcheckbox" data-invoicelineid=5 />

Now say all these checkboxes have been checked in jquery how would I get a list of all the data-invoicelineid? ie. It should contain 3,4,5.

I wrote the following but it only gets the first one.

var checkedInvoiceLineIds = $(".fieldeapprovalcheckbox:checked").attr("data-invoicelineid");
4

2 回答 2

6

你应该使用 map()

var checkedInvoiceLineIds = $(".fieldeapprovalcheckbox:checked").map(function(){
        return $(this).data("invoicelineid");
}).get();

你有一个包含检查值的数组

于 2012-04-12T08:54:59.857 回答
4
var checkedInvoiceLineIds = [];
$(".fieldeapprovalcheckbox:checked").each(function(){
   checkedInvoiceLineIds.push($(this).data("invoicelineid"));
});
于 2012-04-12T08:55:08.623 回答