可以通过遍历 checkbox 的 ID 来判断是否选中,并将选中的 ID 保存到一个数组中。以下是一个示例代码:

// 获取所有 checkbox 元素
var checkboxes = document.querySelectorAll('input[type='checkbox']');

// 定义一个数组保存选中的 ID
var selectedIDs = [];

// 遍历 checkbox 元素
for (var i = 0; i < checkboxes.length; i++) {
  // 判断当前 checkbox 是否选中
  if (checkboxes[i].checked) {
    // 将选中的 ID 添加到数组中
    selectedIDs.push(checkboxes[i].id);
  }
}

// 输出选中的 ID
console.log(selectedIDs);

在上述代码中,我们首先通过 document.querySelectorAll 方法获取到所有 type 为 checkbox 的元素,然后使用 for 循环遍历每个 checkbox 元素。通过判断 checkbox.checked 属性是否为 true 来判断是否选中。如果选中,则将对应的 ID 添加到 selectedIDs 数组中。最后,我们通过 console.log 方法将选中的 ID 输出到控制台。

JavaScript 多选框筛选 - 获取选中 ID 的方法

原文地址: https://www.cveoy.top/t/topic/dxYK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录