简体   繁体   English

如何显示在 data-id 属性中选中的复选框

[英]How to show checkbox checked in data-id attribute

I found this code:我找到了这个代码:

 var checkboxes = $('input[type=checkbox]'); checkboxes.on('change', function() { $('#divfilter').text(function() { return checkboxes.filter(':checked').map(function() { return this.name; }).get() .join(', ') + '.'; }); });
 <input type="checkbox" name="barbecue" id="barbecue" value="oui" class="barbecue" /> <input type="checkbox" name="handicap" id="handicap" value="oui" class="handicap" /> <input type="checkbox" name="animaux" id="animaux" value="oui" class="animaux" /> <div id="divfilter"></div>

It works perfectly but I need the data to be displayed here inside data-namevar="HERE":它工作得很好,但我需要在 data-namevar="HERE" 中显示数据:

<button
  data-namevar="I NEED CHECKBOX CHECKED SHOW HERE"
  class="add-to-cart btn_1 gradient medium full-width">Add to cart <i class="icon_bag_alt"></i>
</button>

How to do it?怎么做?

Can you try this ?你能试试这个吗?

var checkboxes = $('input[type=checkbox]');
checkboxes.on('change', function() {
  $('#divfilter').text(function() {
    return checkboxes.filter(':checked').map(function() {
      return $(this).attr('data-namevar');
    }).get()    
    .join(', ') + '.';
  });
});

And now, how do I display that data exactly in my html?现在,如何在我的 html 中准确显示该数据?

<button
    data-namevar="I NEED CHECKBOX CHECKED NAMES SHOW HERE">
    Add to cart
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM