JS实现全选,全不选

 <script type="text/javascript">
        function selectItem() {
            document.getElementById("chklist").value = "";
            var chblist = document.getElementsByName("chkItem");
            if (chblist.length > 0) {
                var rtn = "";
                for (var i = 0; i < chblist.length; i++) {
                    if (chblist[i].checked) {
                        rtn += chblist[i].value + ",";
                    }
                    else {
                        continue;
                    }
                }
                document.getElementById("chklist").value = rtn.substring(0, rtn.length - 1);
                if (document.getElementById("chklist").value == "") {
                    document.getElementById("chkAll").checked = false;
                }
            }
            else {
                alert("您还没有选择!");
                return;
            }
        }
        function selectall() {
            document.getElementById("chklist").value = "";
            var items = document.getElementsByName("chkItem");
            var rtn = "";
            var itemAll = document.getElementById("chkAll");
            for (var i = 0; i < items.length; i++) {
                items[i].checked = itemAll.checked;
                if (items[i].checked) {
                    rtn += items[i].value + ",";
                }
                else {
                    continue;
                }
            }
            document.getElementById("chklist").value = rtn.substring(0, rtn.length - 1);
        }
  </script>

 

posted @ 2013-06-17 10:08  stevejson  阅读(408)  评论(0编辑  收藏  举报