[英]Adding and removing checkbox values to an array from many groups of checkboxes
I am working on the below code. 我正在下面的代码。 How can I add and remove value from checkbox groups into an array? 如何添加和删除复选框组中的值到数组中?
Apparently I am able to add the values on checking the checkbox but my attempts to remove same value on un-checking the checkbox didn't work! 显然,我能够在选中复选框时添加值,但是我在取消选中复选框时删除相同值的尝试却不起作用!
let opts = []; $('input:checkbox[name=a]').on('change', function(){ if($(this).is(':checked')){ let val = $(this).data('shape'); opts.push(val); console.log(opts); } else{ let val = $(this).data('shape'); opts.filter(a => a !== val); console.log(opts); } }); $('input:checkbox[name=b]').on('change', function(){ if($(this).is(':checked')){ let val = $(this).data('shape'); opts.push(val); console.log(opts); } else{ let val = $(this).data('shape'); opts.filter(a => a !== val); console.log(opts); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="a" data-shape="scb1" /> <input type="checkbox" name="a" data-shape="scb2" /> <input type="checkbox" name="a" data-shape="scb3" /> <input type="checkbox" name="a" data-shape="scb4" /> <input type="checkbox" name="b" data-shape="mcb1" /> <input type="checkbox" name="b" data-shape="mcb2" /> <input type="checkbox" name="b" data-shape="mcb3" /> <input type="checkbox" name="b" data-shape="mcb4" />
let opts = []; $('input:checkbox[name=a],[name=b]').change(function(){ let val = $(this).data('shape'); if($(this).is(':checked')) { opts.push(val); console.log(opts); } else { opts.splice( $.inArray($(this).data('shape'), opts), 1 ); console.log(opts); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="a" data-shape="scb1" > First </input><br> <input type="checkbox" name="a" data-shape="scb2" > Second </input><br> <input type="checkbox" name="a" data-shape="scb3" > Three</input><br> <input type="checkbox" name="a" data-shape="scb4" > Four</input><br> <br> <input type="checkbox" name="b" data-shape="mcb1" >Five</input><br> <input type="checkbox" name="b" data-shape="mcb2" >Six</input><br> <input type="checkbox" name="b" data-shape="mcb3" >Seven</input><br> <input type="checkbox" name="b" data-shape="mcb4" >Eight</input><br>
If you want opts.filter(a => a !== val);
如果要opts.filter(a => a !== val);
to work you have to assign the new value again like: opts = opts.filter(a => a !== val);
要工作,您必须再次分配新值,例如: opts = opts.filter(a => a !== val);
You can also use opts.splice( $.inArray(val, opts), 1 )
; 您还可以使用opts.splice( $.inArray(val, opts), 1 )
; It will remove the selected val
from your array 它将从数组中删除所选的val
Note: you can improve your function by allowing multiple select, $('input:checkbox[name=a],input:checkbox[name=b]')
注意:您可以通过允许多次选择$('input:checkbox[name=a],input:checkbox[name=b]')
来改善功能
Demo 演示
let opts = []; $('input:checkbox[name=a],input:checkbox[name=b]').on('change', function() { if ($(this).is(':checked')) { let val = $(this).data('shape'); opts.push(val); console.log(opts); } else { let val = $(this).data('shape'); opts.splice( $.inArray(val, opts), 1 ) console.log(opts); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="a" data-shape="scb1" /> <input type="checkbox" name="a" data-shape="scb2" /> <input type="checkbox" name="a" data-shape="scb3" /> <input type="checkbox" name="a" data-shape="scb4" /> <input type="checkbox" name="b" data-shape="mcb1" /> <input type="checkbox" name="b" data-shape="mcb2" /> <input type="checkbox" name="b" data-shape="mcb3" /> <input type="checkbox" name="b" data-shape="mcb4" />
Here is a updated version of your code: 这是您代码的更新版本:
$('input:checkbox[name=a],input:checkbox[name=b]').on('change', function() {
let val = $(this).data('shape');
if ($(this).is(':checked')) {
opts.push(val);
} else {
opts.splice( $.inArray(val, opts), 1 )
}
console.log(opts);
});
You could use filter
to remove unchecked data,hope this helps 您可以使用filter
删除未经检查的数据,希望这会有所帮助
let opts = []; $('input[type="checkbox"]').on('change', function() { let val = $(this).data('shape'); if ($(this).is(':checked')) { opts.push(val); console.log(opts); } else { opts = opts.filter(function(elem) { return elem != val; }); console.log(opts); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="a" data-shape="scb1" /> <input type="checkbox" name="a" data-shape="scb2" /> <input type="checkbox" name="a" data-shape="scb3" /> <input type="checkbox" name="a" data-shape="scb4" /> <input type="checkbox" name="b" data-shape="mcb1" /> <input type="checkbox" name="b" data-shape="mcb2" /> <input type="checkbox" name="b" data-shape="mcb3" /> <input type="checkbox" name="b" data-shape="mcb4" />
let opts = []; $('input:checkbox[name=a]').on('change', function() { if ($(this).is(':checked')) { let val = $(this).data('shape'); opts.push(val); console.log(opts); } else { let val = $(this).data('shape'); opts = opts.filter(a => a !== val); console.log(opts); } }); $('input:checkbox[name=b]').on('change', function() { if ($(this).is(':checked')) { let val = $(this).data('shape'); opts.push(val); console.log(opts); } else { let val = $(this).data('shape'); opts = opts.filter(a => a !== val); console.log(opts); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="a" data-shape="scb1" /> <input type="checkbox" name="a" data-shape="scb2" /> <input type="checkbox" name="a" data-shape="scb3" /> <input type="checkbox" name="a" data-shape="scb4" /> <input type="checkbox" name="b" data-shape="mcb1" /> <input type="checkbox" name="b" data-shape="mcb2" /> <input type="checkbox" name="b" data-shape="mcb3" /> <input type="checkbox" name="b" data-shape="mcb4" />
array's filter method return a new array, you should assign to 'opts value' that it will work well 数组的filter方法返回一个新数组,您应该将其分配给“选择值”,以使其正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.