最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Get data-attribute of all checkbox into a string - Stack Overflow

programmeradmin5浏览0评论

I have a list of checkboxes that looks like this:

<input type="checkbox" class="pcb" value="1" data-id="99">
<input type="checkbox" class="pcb" value="2" data-id="98">
<input type="checkbox" class="pcb" value="3" data-id="97">

And originally I only needed the value inside the value attribute of the checked checkbox. I use this javascript/jquery code to do that:

var receiptNos = $("#result input:checkbox:checked").map(function () {
   return $(this).val();
}).get();

Using this code gives me: receiptNos = '1,2,3' Now I need to have another string variable that will hold the content of data-id of all checked checkboxes: receiptNos2 = '99,98,97'

I tried using:

var receiptNos2 = $("#result input:checkbox:checked").attr('data-id').map(function () {
   return $(this).val();
}).get();

but it doesn't work. Any ideas?

I have a list of checkboxes that looks like this:

<input type="checkbox" class="pcb" value="1" data-id="99">
<input type="checkbox" class="pcb" value="2" data-id="98">
<input type="checkbox" class="pcb" value="3" data-id="97">

And originally I only needed the value inside the value attribute of the checked checkbox. I use this javascript/jquery code to do that:

var receiptNos = $("#result input:checkbox:checked").map(function () {
   return $(this).val();
}).get();

Using this code gives me: receiptNos = '1,2,3' Now I need to have another string variable that will hold the content of data-id of all checked checkboxes: receiptNos2 = '99,98,97'

I tried using:

var receiptNos2 = $("#result input:checkbox:checked").attr('data-id').map(function () {
   return $(this).val();
}).get();

but it doesn't work. Any ideas?

Share Improve this question asked Jul 25, 2016 at 3:06 super-usersuper-user 1,0574 gold badges18 silver badges41 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

Instead return $(this).val(); you can use return $(this).data('id');

var receiptNos2 = $("#result input:checkbox:checked").map(function () {
    return $(this).data('id')
}).get();
发布评论

评论列表(0)

  1. 暂无评论