簡體   English   中英

如何顯示在 data-id 屬性中選中的復選框

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

我找到了這個代碼:

 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>

它工作得很好,但我需要在 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>

怎么做?

你能試試這個嗎?

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(', ') + '.';
  });
});

現在,如何在我的 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