首页 > web前端 > js教程 > jquery实现checkbox全选全不选的简单实例

jquery实现checkbox全选全不选的简单实例

PHPz
发布: 2018-09-30 17:16:58
原创
1254 人浏览过

本篇文章主要是对jquery实现checkbox全选全不选的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助。

demo一:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<%@ taglib uri="/WEB-INF/tlds/test.tld" prefix="pig"%>

<%

 String path = request.getContextPath();

 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path;

%>

<!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>

        <title></title>

        <script type="text/javascript" src="<%=path %>/scripts/jquery.js"></script>

        <script type="text/javascript">

            function checkSubmit() {

 

                document.Search.submit();

            }

 

            function fnull() {

                var obj = document.getElementById("goPage");

                var index = obj.selectedIndex; // 选中索引

                var text = obj.options[index].text; // 选中文本

                var go = obj.options[index].value; // 选中值

 

                //alert(go);

                gopage(go);

            }

 

            function gopage(page) {

                var numberpg = document.getElementById("numberpg").value;

                document.getElementById("prepage").value = numberpg;

                document.getElementById("currPage").value = page;

                checkSubmit();

            }

 

            function px() {

                var numberpg = document.getElementById("numberpg").value;

                gopage(1);

            }

 

            function driction(id) {

                document.s_result_form1.s_result_form1_id.value = id;

                document.s_result_form1.submit();

            }

 

            $(function() {

 

                $("td").css("height", "22px");

 

                $("#refresh").bind("click", function() {

                    //location.reload();

                    location = "wlan!querySurveyWlan.do";

                });

 

                $(".dg_alternatingitemstyle").bind("mouseover", function() {

                    $(this).children().css("height", "22px");

                });

 

                $(".dg_alternatingitemstyle").bind("mouseout", function() {

                    $(this).children().css("height", "22px");

                });

            });

        </script>

        <style type="text/css">

            <!-- .f {

                font-size: 12px;

            }

             

            -->

        </style>

    </head>

 

    <body id="master">

 

        <div id="mainareacontent">

            <div class="mainarea">

                <div class="dataarea">

                    <table width="100%" cellpadding="0" cellspacing="0" class="title">

                        <tr>

                            <th width="1268">

                                <span>WLAN调查管理</span>

                            </th>

                            <td width="26" class="null">

                                <input type="image" name="import" src="<%=path %>/themes/default/btn_sc.gif" onclick="deletes()" alt="勾选批量删除" />

                            </td>

                            <td width="16" class="null" align="center">

                                <input type="image" name="export" src="<%=path %>/themes/default/btn_exp.gif" onclick="window.location = &#39;wlan!hotExport.do&#39;" alt="导出" />

                            </td>

                            <td width="16" class="null">

                                <img id="refresh" src="<%=path %>/themes/default/btn_sx.gif" width="50" height="20" alt="刷新" />

                            </td>

                        </tr>

                    </table>

                    <!----------- 数据列表 ------------------>

                    <div class="datagrid">

                        <div class="search" style="margin-top:0px;">

                            <form action="wlan!querySurveyWlan.do" method="post" onsubmit="return false;" name="Search" id="Search">

                                <table cellpadding="0" cellspacing="0" width="97%">

                                    <tr>

                                        <td>记录日期:</td>

                                        <td><input type="text" name="msgDate" id="msgDate" value="${msgDate}" /></td>

                                        <td>热点类型:</td>

                                        <td>

                                            <s:select list="typeMap" name="msgType" value="msgType" theme="simple" />

                                        </td>

                                        <td>上网类型:</td>

                                        <td>

                                            <s:select list="areaMap" name="msgOnlineType" value="msgOnlineType" theme="simple" />

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>手机号码:</td>

                                        <td>

                                            <input type="text" name="msgMobileno" id="msgMobileno" value="${msgMobileno}" />

                                        </td>

                                        <td>地址建议:</td>

                                        <td>

                                            <input type="text" name="msgAddress" id="msgAddress" value="${msgAddress}" />

                                        </td>

                                        <td style="text-align:left" colspan="2"><img style="width:50; height:20;

border:0; cursor: pointer;" onclick="checkSubmit()" src="<%=path %>/themes/default/btn_search.gif" /></td>

                                    </tr>

                                </table>

                                <input type="hidden" name="prepage" id="prepage" value="${prepage}" />

                                <input type="hidden" name="currPage" id="currPage" value="${currPage}" />

                            </form>

                        </div>

 

                        <table id="list" width="100%" cellpadding="0" cellspacing="0" class="dg_borderstyle" border="1" bordercolor="#1C568A">

                            <tr>

                                <th width="3%" style="background-color:#9FBFE3">

                                    <div align="center" title="全选/全不选"><input id="allSelect" name="allSelect" type="checkbox" onclick="selectAll()" title="全选/全不选" /></div>

                                </th>

                                <th width="10%" style="background-color:#9FBFE3">

                                    <div align="center">记录日期</div>

                                </th>

                                <th width="10%" style="background-color:#9FBFE3">

                                    <div align="center">热点类型</div>

                                </th>

                                <th width="13%" style="background-color:#9FBFE3">

                                    <div align="center">上网类型</div>

                                </th>

                                <th width="12%" style="background-color:#9FBFE3">

                                    <div align="center">手机号码</div>

                                </th>

                                <th width="12%" style="background-color:#9FBFE3">

                                    <div align="center">地址建议</div>

                                </th>

                            </tr>

                            <s:iterator value="surveyList" id="dto" status="sta">

                                <tr onMouseOver="MOver(this)" onMouseOut="Mout(this)" class="dg_alternatingitemstyle">

                                    <td align="center" class="f">

                                        <c:choose>

                                            <c:when test="1==1">

                                                <input id="ck" name="ck" type="checkbox" onclick="cancelCKSelect()" disabled="disabled" />

                                            </c:when>

                                            <c:otherwise>

                                                <input id="ck" name="ck" type="checkbox" onclick="cancelCKSelect()" />

                                            </c:otherwise>

                                        </c:choose>

                                        <input id="ckvalue${sta.count-1}" name="ckvalue${sta.count-1}" type="hidden" value="${dto.id}" />

                                    </td>

                                    <td align="center" class="f">

                                        ${dto.recmakedate}

                                    </td>

                                    <td align="center" class="f">

                                        ${dto.type}

                                    </td>

                                    <td align="center" class="f">

                                        ${dto.onlinetype}

                                    </td>

                                    <td align="center" class="f">

                                        ${dto.mobileno}

                                    </td>

                                    <td align="center" class="f">

                                        ${dto.address}

                                    </td>

                                </tr>

                            </s:iterator>

                        </table>

                        <s:if test="surveyList.size>0">

                            <table width="98%" cellpadding="0" cellspacing="0" class="dg_pagestyle">

                                <tr>

                                    <th>第${currPage}页 | 共${countPage}页/${countNum}条记录</th>

                                    <td>每页

                                        <select id="numberpg" name="numberpg" onChange="px(this.options

 

[this.selectedIndex].value)">

                                            <option <c:if test="${prepage==100}">selected="selected"</c:if>

 

                                                value="100">100</option>

                                            <option <c:if test="${prepage==300}">selected="selected"</c:if>

 

                                                value="300">300</option>

                                            <option <c:if test="${prepage==500}">selected="selected"</c:if>

 

                                                value="500">500</option>

                                        </select>

                                        条记录 | 第

                                        <select name="goPage" id="goPage">

                                            <s:iterator begin="1" end="countPage" status="stu">

                                                <c:choose>

                                                    <c:when test="${stu.count==currPage}">

                                                        <option value=&#39;${stu.count}&#39; selected=&#39;selected&#39;>${stu.count}</option>

                                                    </c:when>

                                                    <c:otherwise>

                                                        <option value="${stu.count}">${stu.count}</option>

                                                    </c:otherwise>

                                                </c:choose>

                                            </s:iterator>

                                        </select>

                                        <!-- <input type="text" style="width:25px;" id="goPage" value="${currPage}" onkeydown="if(event.keyCode==13){fnull();return false;}" style="IME-MODE: disabled; WIDTH: 60px; HEIGHT: 15px" onkeyup="this.value=this.value.replace(/\D/g,&#39;&#39;)"  onafterpaste="this.value=this.value.replace(/\D/g,&#39;&#39;)" />-->页

                                        <a onClick="fnull()" id="go" style="text-decoration:none;">

                                            <img border="0" src="<%=path %>/themes/default/btn_go.gif" />

                                        </a>

                                        <a style="text-decoration:none;" onClick="gopage(1)">

                                            <img border="0" src=&#39;<%=path %>/themes/default/btn_sy.gif&#39; />

                                        </a>

                                        <c:if test="${currPage!=1}">

                                            <a style="text-decoration:none;" onClick="gopage(${currPage-1})">

                                                <img border="0" src=&#39;<%=path %>/themes/default/btn_syy.gif&#39; />

                                            </a>

                                        </c:if>

                                        <c:if test="${currPage!=countPage}">

                                            <a style="text-decoration:none;" onClick="gopage(${currPage+1})">

                                                <img border="0" src=&#39;<%=path %>/themes/default/btn_xyy.gif&#39; />

                                            </a>

                                        </c:if>

                                        <a style="text-decoration:none;" onClick="gopage(${countPage})">

                                            <img border="0" src=&#39;<%=path %>/themes/default/btn_wy.gif&#39; />

                                        </a>

                                    </td>

                                </tr>

                            </table>

                        </s:if>

                        <s:else>

                            <center>当前查询没有数据!</center>

                        </s:else>

                    </div>

                </div>

            </div>

        </div>

    </body>

</html>

<script>

    $(function() {

 

    });

    function selectAll() {

        var ck = $("input[name=ck]");

        var currSelect = $("input[name=allSelect][checked]").val();

 

        $.each(ck, function(i) {

            //alert(ck[i].disabled)

            if(!ck[i].disabled) {

                ck[i].checked = currSelect;

            }

        });

    }

    function cancelCKSelect() {

        var ck = $("input[name=ck]");

        var r = true;

        $.each(ck, function(i) {

            if(!ck[i].checked && !ck[i].disabled) {

                r = false;

                return false;

            }

        });

        $(&#39;#allSelect&#39;).attr(&#39;checked&#39;, r);

    }

    function deletes() {

        var delId = "";

        var ck = $("input[name=ck]");

 

        $.each(ck, function(i) {

            if(ck[i].checked && !ck[i].disabled) {

                delId += "&#39;" + $("#ckvalue" + i).val() + "&#39;,";

            }

        });

        delId = delId.substring(0, delId.lastIndexOf(","));

        if(delId != "") {

            var flag = window.confirm("您确定删除吗?");

            if(!flag) {

                return;

            }

            window.location = "wlan!hotDelete.do?hotWlanId=" + delId;

        } else {

            alert(&#39;请选择需要删除的数据!&#39;);

        }

    }

</script>

登录后复制

demo二:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<script src="jquery-1.6.2.min.js"></script>

<input type="checkbox" id="ckAll" />check all<br />

<input type="checkbox" name="sub" />1<br />

<input type="checkbox" name="sub"/>2<br />

<input type="checkbox" name="sub"/>3<br />

<input type="checkbox" name="sub"/>4<br />

<script>

  $("#ckAll").click(function() {

    $("input[name=&#39;sub&#39;]").prop("checked", this.checked);

  });

 

  $("input[name=&#39;sub&#39;]").click(function() {

    var $subs = $("input[name=&#39;sub&#39;]");

    $("#ckAll").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);

  });

</script>

登录后复制

以上就是本章的全部内容,更多相关教程请访问jQuery视频教程

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板