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

rdlzhqv9  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(137)

数组:

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代码

<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">
9wbgstp7

9wbgstp71#

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);
            })
        });

相关问题