指定查询范围

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type ="text/javascript">
        $(function () {
            $("#btn1").click(function () {
                //获取页面第一个表单
                var form = $("form:first");

                //指定选择器的查询上下文为form
                //如果没有为选择器指定上下文(只传一个参数)jq会到dom树里面查找
                //如果加了上下文(传递了两个参数),那么jq就到第二个参数里查找
                var checkedBox = $("input[name=chkl]:checked", form); //到指定的上下文里查找
                alert(checkedBox.length);
            });
           
        });
    </script>
</head>
<body>
    <form action="" method ="post">
       用户名:
        <input type ="text" />
        密码:
        <input type ="password" />
        <input type ="checkbox" name="chkl" />
         <input type ="checkbox" name="chkl" />
         <input type ="checkbox" name="chkl" />
    </form>
    <input type ="button" id="btn1" value="点击"/>
</body>
</html>

  

原文地址:https://www.cnblogs.com/sumg/p/3787718.html