简体   繁体   English

数组和复选框,我希望根据数组值选中/取消选中框,并在有人选中任何框并更新表单时更新数组

[英]array and checkboxes, I want boxes to checked/unchecked based on array values and also update array when someone check any box and update the form

Array:大批:

0: {scat_id: '1', scat_name: 'Domain Knowledge', scat_identifier: 'domain knowledge', sname_id: '2', sname_cat_id: '1', …}
1: {scat_id: '1', scat_name: 'Domain Knowledge', scat_identifier: 'domain knowledge', sname_id: '4', sname_cat_id: '1', …}
5: {scat_id: '2', scat_name: 'Type of Testing', scat_identifier: 'type-of-testing', sname_id: '8', sname_cat_id: '2', …}
6: {scat_id: '2', scat_name: 'Type of Testing', scat_identifier: 'type-of-testing', sname_id: '23', sname_cat_id: '2', …}

HTML Code HTML 代码

<input type="checkbox" class="custom-control-input" id="bfsi" ng-model="skill_set.bfsi" value="2">
<input type="checkbox" class="custom-control-input" id="ecommerce" ng-model="skill_set.ecommerce" value="4">
<input type="checkbox" class="custom-control-input" id="api" ng-model="skill_set.api" value="26">
<input type="checkbox" class="custom-control-input" id="game" ng-model="skill_set.game" value="23">
var checkboxes = document.querySelectorAll("input[type=checkbox][name=NAME OF THE CHECKBOXES]");
let enabledCatFilters = []     
 checkboxes.forEach(function (checkbox) {
                checkbox.addEventListener('change', function () {
                    enabledCatFilters =
                        Array.from(checkboxes) // Convert checkboxes to an array to use filter and map.
                            .filter(i => i.checked) // Use Array.filter to remove unchecked checkboxes.
                            .map(i => parseInt(i.value)) // Use Array.map to extract only the checkbox values from the array of objects.
               alert(enabledCatFilters);
            })
        });

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

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