从阵列中删除未选中的复选框
Remove unchecked checkbox from array
我有一堆复选框:
<div id="other-products">
<input type="checkbox" name="First" value="1">
<input type="checkbox" name="Second" value="2">
<input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>
像这样的JS代码,它生成检查输入的所有名称。
$('#other-products').on('change', function() {
var selectedProducts = [];
$('#other-products input[type="checkbox"]:checked').each(function() {
selectedProducts.push($(this).attr('name'));
var showtimesAsString = selectedProducts.join(', ');
$('.list-products').html(showtimesAsString);
});
});
问题是,当我选择几个复选框,然后取消选择所有复选框时,最后一个未选中的复选框标题显示在 .list-products 中。知道为什么它不是空的吗?谢谢!
尝试在循环之外重置列表产品的 html 内容。
https://jsfiddle.net/99x50s2s/188/
$('#other-products').on('change', function() {
var selectedProducts = [];
var listProductsCtrl = $('.list-products');
listProductsCtrl.html(''); //reset the values here
$('#other-products input[type="checkbox"]:checked').each(function() {
selectedProducts.push($(this).attr('name'));
var showtimesAsString = selectedProducts.join(', ');
listProductsCtrl.html(showtimesAsString);
});
});
除了导致脚本无法按预期工作外,$.each
showtimesAsString
设置效率低下。移动创建字符串的 2 行,并将.html()
设置在 $.each()
之外。
$('#other-products').on('change', function() {
var selectedProducts = [];
$('#other-products input[type="checkbox"]:checked').each(function() {
selectedProducts.push($(this).attr('name'));
});
var showtimesAsString = selectedProducts.join(', ');
$('.list-products').html(showtimesAsString);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="other-products">
<input type="checkbox" name="First" value="1">
<input type="checkbox" name="Second" value="2">
<input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>
您可以将代码更改为如下所示的内容:
$('#other-products').on('change', function() {
var selectedProducts = $('#other-products input[type="checkbox"]:checked');
var showtimesAsString = "";
selectedProducts.each(function() {
showtimesAsString += $(this).attr("name") + ",";
});
$('.list-products').html(showtimesAsString);
});
https://jsfiddle.net/5bu0sqjf/
$('#other-products').on('change', function() {
var selectedProducts = [];
$('.list-products').html('');
$('#other-products input[type="checkbox"]:checked').each(function() {
selectedProducts.push($(this).attr('name'));
var showtimesAsString = selectedProducts.join(', ');
$('.list-products').html(showtimesAsString);
});
});
您可以使用
jQuery的map()
函数来真正简化代码,避免手动重置数组和输出字符串。
$(function() {
$('#other-products').on('change', function() {
var names = $('#other-products input[type="checkbox"]:checked').map(function() {
return $(this).attr('name');
}).get();
$('.list-products').html(names.join(', '));
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="other-products">
<input type="checkbox" name="First" value="1">
<input type="checkbox" name="Second" value="2">
<input type="checkbox" name="Third" value="3">
</div>
<span class="list-products"></span>
相关文章:
- 如何删除'被按压'state from复选框
- 通过选中和取消选中复选框来添加和删除数组中的值
- 取消选中复选框时,从收音机中删除“选中”
- 基于复选框切换删除多边形
- 取消选中特定复选框时,React切换不删除的选择字段
- 单击删除时,从表中删除与选定复选框相对应的行
- 添加和删除复选框,取决于输入值
- Knockoutjs通过选中复选框来删除多个
- Twitter引导程序:删除/切换类似复选框的按钮组的活动状态
- 在复选框取消选中事件中从HTML下拉列表中删除项目
- 通过选中复选框删除多行
- 如何禁用和启用删除按钮,如果未在 GridView 中选中复选框由 Jquery
- 选中复选框时用 innerHTML 添加的文本,取消选中时不会删除
- 如何使用 javascript 删除复选框
- 如何在 JSP 中的“单击”复选框时创建删除按钮
- 使用jquery选中/取消选中复选框添加/删除值
- Salesforce-使用jQuery使用复选框删除表行
- 使用复选框删除本地内存中存储的项目Jquery
- 使用php或javascript中的复选框删除
- OpenLayers复选框删除标签