javascript - 如何显示在 data-id 属性中选中的复选框
问题描述
我找到了这段代码:
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(', ') + '.';
});
});
推荐阅读
- javafx - 如何在超链接单击时获取 tableview 项目
- postgresql - Postgres LIKE %% 语法错误
- spring - 我如何知道 SSL 证书链正在被验证?
- r - 使用 R 包中的 `/exec` 目录用于 R 脚本以进行可重复的研究
- webpack - 为什么 webpack 的事件监听器钩子叫做 `plugin` 而不是 `on` 之类的?
- gmail - 是否可以链接到 GMail 委托帐户中的电子邮件
- python - 使用精明边缘检测的图像裁剪
- sql - 如何不在 CASE 语句中使用 WHERE?
- visual-studio - 将 VS 2017 的任务列表(令牌列表)设置回默认值?
- ios - SceneKit:在运行时从模型创建 SCNNode