一个网上很多的例子如下:
Meta test
Box").attr( <span class="hljs-comment">// 全不选
$(<span class="hljs-string">"#btnCheckNone").bind(<span class="hljs-string">"click",<span class="hljs-literal">false);
});
<span class="hljs-comment">// 反选
$(<span class="hljs-string">"#btnCheckReverse").bind(<span class="hljs-string">"click",<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">() {
$(<span class="hljs-string">"[name = chkItem]:check<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>").each(<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">() {
$(<span class="hljs-keyword">this).attr(<span class="hljs-string">"checked",!$(<span class="hljs-keyword">this).attr(<span class="hljs-string">"checked"));
});
});
<span class="hljs-comment">// 全不选
$(<span class="hljs-string">"#btnSubmit").bind(<span class="hljs-string">"click",<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">() {
<span class="hljs-keyword">var result = <span class="hljs-keyword">new <span class="hljs-built_in">Array();
$(<span class="hljs-string">"[name = chkItem]:check<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>").each(<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-params">() {
<span class="hljs-keyword">if ($(<span class="hljs-keyword">this).is(<span class="hljs-string">":checked")) {
result.push($(<span class="hljs-keyword">this).attr(<span class="hljs-string">"value"));
}
});
alert(result.join(<span class="hljs-string">","));
});
});
<span class="hljs-tag"></<span class="hljs-title">script>
<span class="hljs-tag"></<span class="hljs-title">head>
<span class="hljs-tag"><<span class="hljs-title">body>
<span class="hljs-tag"><<span class="hljs-title">div>
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"今日话题" />今日话题
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"视觉焦点" />视觉焦点
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"财经" />财经
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"汽车" />汽车
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"科技" />科技
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"房产" />房产
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">name=<span class="hljs-value">"chkItem" <span class="hljs-attribute">type=<span class="hljs-value">"checkBox" <span class="hljs-attribute">value=<span class="hljs-value">"旅游" />旅游
<span class="hljs-tag"></<span class="hljs-title">div>
<span class="hljs-tag"><<span class="hljs-title">div>
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">id=<span class="hljs-value">"btnCheckAll" <span class="hljs-attribute">type=<span class="hljs-value">"button" <span class="hljs-attribute">value=<span class="hljs-value">"全选" />
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">id=<span class="hljs-value">"btnCheckNone" <span class="hljs-attribute">type=<span class="hljs-value">"button" <span class="hljs-attribute">value=<span class="hljs-value">"全不选" />
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">id=<span class="hljs-value">"btnCheckReverse" <span class="hljs-attribute">type=<span class="hljs-value">"button" <span class="hljs-attribute">value=<span class="hljs-value">"反选" />
<span class="hljs-tag"><<span class="hljs-title">input <span class="hljs-attribute">id=<span class="hljs-value">"btnSubmit" <span class="hljs-attribute">type=<span class="hljs-value">"button" <span class="hljs-attribute">value=<span class="hljs-value">"提交" />
<span class="hljs-tag"></<span class="hljs-title">div>
<span class="hljs-tag"></<span class="hljs-title">body>
<span class="hljs-tag"></<span class="hljs-title">html>
运行后,在火狐下面发现一个问题百思不得其解
问题描述:第一次点全选可以,然后点击全不选,接着再点击全选、全不选、反选就没了反应,后来用其他浏览器发下可以,所以感觉是兼容性的问题,后来查阅资料发现果然是的,参考地址
解决方法:将attr换为prop即可,经过验证各个浏览器都是好的,官网说明是在1.6之后建议用prop,在此记录以备后用
下为效果图
图片描述">