JS获取复选框被选中的值

< head >
< script src= "https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js" > < / script >
</ head >

< body >
< div id= "chkDiv" >
< input type= radio value= "1" />
< input type= radio value= "0" />
< input type= button value= "TEST" onclick= "test();" />
</ div >
</ body >
< script >
function test() {
$( "#chkDiv :radio").each( function () {
console.log($( this).is( ":checked"));
console.log($( this).val());
var trueSex = $( this).val();
console.log( 'trueSex', trueSex);
})
}
< / script >

< body >
< div >
< input type= "checkbox" value= "遍历1" />1
< input type= "checkbox" value= "遍历2" />2
< input type= "checkbox" value= "遍历3" />3
< input type= "checkbox" value= "遍历4" />4
< input type= "checkbox" value= "遍历5" />5
< br >
< input type= "button" id= "btn" value= "遍历" />
</ div >
< script >
// 通过type属性遍历:
$(document).ready( function () {
$( "#btn").click( function () {
var opt = "";
$( "input[type='checkbox']").each( function () {
if ($( this).is( ":checked")) {
opt = "选中";
console.log($( this).val())
} else {
opt = "未选中";
}
});
});
});
< / script >
</ body >

猜你喜欢

转载自blog.csdn.net/kyunsoo/article/details/80733576
今日推荐