jquery traversal method can get the check box to the selected value
12 |
$( "input:checkbox:checked" ).each( function (index,element)); // 为所有选中的复选框执行函数,函数体中可以取出每个复选框的值 $( "input:checkbox:checked" ).map( function (index,domElement)); // 将所有选中的复选框通过函数返回值生成新的jQuery 对象 |
Example Demo: Click the button to get the checkbox's selected value
Creating HTML Elements
12345678 |
<
div class
=
"box"
>
<
span
>点击按钮获取checkbox的选中值:</
span
>
<
div
class
=
"content"
>
<
input
type
=
‘checkbox‘
name
=
‘message‘
value
=
‘1‘
/>发送短信
<
input type
=
‘checkbox‘
name
=
‘message‘
value
=
‘2‘
/>发送邮件
</
div
>
<
input type
=
"button"
value
=
"提交"
>
</
div
>
|
Set CSS Styles
123 |
div.box{
width
:
300px
;
padding
:
20px
;
margin
:
20px
;
border
:
4px
dashed
#ccc
;}
div.box>span{
color
:
#999
;
font-style
:
italic
;}
div.content{
width
:
250px
;
margin
:
10px
0
;
padding
:
20px
;
border
:
2px
solid #ff6666
;}
|
Writing jquery Code
12345678 |
$(
function
(){
$(
"input:button"
).click(
function
() {
text = $(
"input:checkbox[name=‘message‘]:checked"
).map(
function
(index,elem) {
return
$(elem).val();
}).get().join(
‘,‘
);
alert(
"选中的checkbox的值为:"
+text);
});
});
|
Observation effect
How does jquery get the values of multiple check boxes?