在js中获取 input checkbox里选中的多个值
思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中)。下面进行实例演示:
1.html中展示:
<
input
type
=
"checkbox"
name
=
"test"
value
=
"1"
/><
span
>1</
span
>
<
input
type
=
"checkbox"
name
=
"test"
value
=
"2"
/><
span
>2</
span
>
<
input
type
=
"checkbox"
name
=
"test"
value
=
"3"
/><
span
>3</
span
>
<
input
type
=
"checkbox"
name
=
"test"
value
=
"4"
/><
span
>4</
span
>
<
input
type
=
"checkbox"
name
=
"test"
value
=
"5"
/><
span
>5</
span
>
<
input
type
=
'button'
value
=
'提交'
onclick
=
"fun()"
/>
2.js中实现:
function
fun(){
obj = document.getElementsByName(
"test"
);
check_val = [];
for
(k
in
obj){
if
(obj[k].checked)
check_val.push(obj[k].value);
}
alert(check_val);
}