一、使用jQuery
代码如下:
var array="";
$("input:checkbox[name='"+name+"']:checked").each(function(i){
if(0==i){
array=$(this).val();
}else{
array+=(","+$(this).val());
}
});
alert(array);
二、使用JS
代码如下:
<input type="checkbox" name="color" id="" value="红">红
<input type="checkbox" name="color" id="" value="黄">黄
<input type="checkbox" name="color" id="" value="蓝">蓝
<input type="checkbox" name="color" id="" value="绿">绿
<hr>
<input type="button" value="点击" id="but">
<script>
document.getElementById("but").οnclick=function(){
var ele=document.getElementsByName("color");
var val="";
for(var item of ele){
(item.checked==true)&&(val+=item.value)
}
console.log(val);
}
</script>