https://zhidao.baidu.com/question/203897221.html
思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中)。下面进行实例演示:
1、HTML结构
<</code>input type = "checkbox" name = "test" value = "1" /><</code>span >1</</code>span >
<</code>input type = "checkbox" name = "test" value = "2" /><</code>span >2</</code>span >
<</code>input type = "checkbox" name = "test" value = "3" /><</code>span >3</</code>span >
<</code>input type = "checkbox" name = "test" value = "4" /><</code>span >4</</code>span >
<</code>input type = "checkbox" name = "test" value = "5" /><</code>span >5</</code>span >
<</code>input type = 'button' value = '提交' onclick = "fun()" />
|
2、javascript代码
function fun(){
obj = document.getElementsByName( "test" );
check_val = [];
for (k in obj){
if (obj[k].checked)
check_val.push(obj[k].value);
}
;
}
|
3、演示效果
