jQuery實現CheckBox全選、全不選

jopen 9年前發布 | 1K 次閱讀 JavaScript jQuery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title>jQuery實現CheckBox全選、全不選</title>
<script src="http://code.jquery.com/jquery-1.4.4.min.js&quot; type="text/javascript"></script>    <script type="text/javascript">
        $(function() {
           $("#checkAll").click(function() {
                $('input[name="subBox"]').attr("checked",this.checked); 
            });
            var $subBox = $("input[name='subBox']");
            $subBox.click(function(){
                $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
            });
        });
    </script>

</head> <body> <div> <input id="checkAll" type="checkbox" />全選 <input name="subBox" type="checkbox" />項1 <input name="subBox" type="checkbox" />項2 <input name="subBox" type="checkbox" />項3 <input name="subBox" type="checkbox" />項4 </div> </body> </html></pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!