一、获取两个不同name属性的checkbox(有限枚举)

<input id="name_checkbox" style="margin-left: 5%; zoom: 150%;" name="nameBox" type="checkbox" value="1" />
<input id="code_checkbox" style="margin-left: 5%; zoom: 150%;" name="codeBox" type="checkbox" value="2" />
1、JS获取
var name_checkbox="";
var code_checkbox="";
if(document.getElementsByName("nameBox")[0].checked){
name_checkbox=document.getElementsByName("nameBox")[0].value;
}
if(document.getElementsByName("codeBox")[0].checked){
code_checkbox=document.getElementsByName("codeBox")[0].value;
}

if(name_checkbox=="" && code_checkbox==""){
//TODO ...
return;
}

2、jQuery获取

待定

​$​​(‘input[name=“chk”]:checked’)

二、获取相同name属性的选中checkbox

<input id="name_checkbox" style="margin-left: 5%; zoom: 150%;" name="chk" type="checkbox" value="1" />
<input id="code_checkbox" style="margin-left: 5%; zoom: 150%;" name="chk" type="checkbox" value="2" />
1、JS获取
function select(){
var chk_value = [];//定义一个数组
var nameArr = document.getElementsByName("chk");
for(var i = 0 ;i < nameArr.length; i++){
chk_value.push(nameArr[i].value);
}
return chk_value;
}

2、jQuery获取
<font color=#008000 > //选中所有</font>
function selectAll(){
$("input[name='chk']").each(function(){
// $(this).attr("checked", true);
this.checked=true;
});
}
<font color=#008000 >//清空所有</font>
function selectNull(){
`$`("input[name='chk']").each(function(){
this.checked=false;
});
}
<font color=#008000 >//默认选中指定checkbox</font>
function select1(){
selectNull(); //先清空选项
var sel = [1,2]; //数组值和checkbox的value属性一一对应
for(var i=0;i<sel.length;i++){
`$`("input[name='chk']").each(function(){
if ($(this).val() == sel[i]) {
this.checked=true;
}
});
}
}
<font color=#008000 >//遍历获取所有选中的checkbox</font>
function getVal(){
var chk_value =[];//定义一个数组
`$`('input[name="chk"]:checked').each(function(){//遍历每一个名字为nodes的复选框,其中选中的执行函数
chk_value.push($(this).val());//将选中的值添加到数组chk_value中
});
var groups = chk_value.join(",");
return groups;
}