You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量选中所有name属性属于question[0]系列的复选框

question[0]分组复选框全选实现方案

所有需要选中的复选框name属性均以question[0]为前缀,直接使用CSS属性前缀选择器即可精准覆盖从question[0][][][]到question[0][3][6][9]的全部目标选项,不会误选其他分组(如question[1])的复选框,无需编写复杂的范围匹配逻辑。


原生JavaScript实现

// 全选方法
function selectAllQuestion0() {
  const targetCheckboxes = document.querySelectorAll('input[type="checkbox"][name^="question[0]"]');
  targetCheckboxes.forEach(item => {
    item.checked = true;
  })
}

// 配套取消全选方法(按需使用)
function unselectAllQuestion0() {
  const targetCheckboxes = document.querySelectorAll('input[type="checkbox"][name^="question[0]"]');
  targetCheckboxes.forEach(item => {
    item.checked = false;
  })
}

绑定到全选按钮即可使用:

<button onclick="selectAllQuestion0()">全选第一题选项</button>

jQuery实现

如果项目已引入jQuery,可以用更简洁的写法:

// 全选
$('#selectAllQ0').click(function() {
  $('input:checkbox[name^="question[0]"]').prop('checked', true);
})

// 取消全选
$('#unselectAllQ0').click(function() {
  $('input:checkbox[name^="question[0]"]').prop('checked', false);
})

注意:jQuery 1.6及以上版本操作表单选中状态请使用prop()方法,不要用attr(),否则会出现状态更新异常的问题。


内容的提问来源于stack exchange,提问作者Sajith Madhubashana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 13:21:30