首页 > 技术文章 > jquery 取指定class下的input checkbox选中的值

llkbk 2018-12-19 11:51 原文

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>属性选择器学习</title>
    <script language="JavaScript" type="text/javascript" src="http://www.jq22.com/jquery/jquery-3.3.1.js"></script>
    <script language="javascript" type="text/javascript">
        //使用jquery加载事件
        $(document).ready(function (){
            $("#btn1").click(function (){var che = [];
          // 取name getElementsByName("ccq")
obj
= document.getElementsByClassName("c1"); console.log(obj) check_val = []; for(k in obj){ if(obj[k].checked) check_val.push(obj[k].value); } console.log(check_val) console.log($("input[class=c1]:checked").length); // alert($("input[type=checkbox]:checked").length); // $('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked') // 选name 则是$("input[name=ccq]:checked")
$.each($("input[class=c1]:checked"),function(i){ // window.alert("你选了:"+ // $('input[type=checkbox]:checked').length+"个,其中有:"+$(this).val()); che.push($(this).val()); console.log(i); }); console.log(che); });</script> </head> <body> <form method="post" action=""> <input class="c1" name="ccq" type="checkbox" value="美女" />美女 <input class="c1" name="ccq" type="checkbox" value="美男" />美男 <input class="c1" name="ccq" type="checkbox" value="大爷" />大爷 <input class="c2" name="ccq" type="checkbox" value="大妈" />大妈 <br> <input type="radio" value="男" /><input type="radio" value="女" /><br> <select id="zhiwei" size="5" multiple="multiple"> <option>PHP开发工程师</option> <option>数据库管理员</option> <option>系统分析师</option> <option>保安</option> </select> <select id="xueli"> <option>大专</option> <option>中专</option> <option>小学</option> </select> </form> <div style="clear:both;"> <input id="btn1" type="button" value="" /><br> </div> </body> </html>

 

 $.each($("input[class=c1]:checked"),function(i){

推荐阅读