jQuery中獲取checkbox選中項(xiàng)等操作及注意事項(xiàng)
2. checkbox選項(xiàng)的全選 反選操作
用于測試的checkbox代碼段:
<div>
<input type="checkbox" name="abc" value="一年級" id="in1" checked="checked" /><label for="in1">一年級</label>
<input type="checkbox" name="abc" value="二年級" id="in2" /><label for="in2">二年級</label>
<input type="checkbox" name="abc" value="三年級" id="in3" /><label for="in3">三年級</label>
<input type="checkbox" name="abc" value="四年級" id="in4" /><label for="in4">四年級</label>
<input type="checkbox" name="abc" value="五年級" id="in5" /><label for="in5">五年級</label>
<input type="checkbox" name="abc" value="六年級" id="in6" /><label for="in6">六年級</label>
<input type="checkbox" name="abc" value="七年級" id="in7" /><label for="in7">七年級</label>
<input type="checkbox" name="abc" value="八年級" id="in8" /><label for="in8">八年級</label>
</div>
一:首先來說第一點(diǎn),獲取checkbox的選中項(xiàng)。網(wǎng)上搜到的大部分方法使用each來獲取:
$("input[name='checkbox'][checked]").each(function () {
alert(this.value);
})
IE下的測試效果(我的是IE10):
IE10下的效果:
chrome瀏覽器下的效果:
通過在google上搜索,找到了原因:
網(wǎng)址: Jquery 選中多少個(gè)input CheckBox問題,IE正常,F(xiàn)F和Chrome無法取到值
因?yàn)槲矣玫膉query版本是1.7.2的,所以這里我得用 :checked 來獲取才行,修改后的代碼:
//獲取選中項(xiàng)
$('#huoqu2').click(function () {
$('#show').html("");
$("input[name='abc']:checked").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
在chrome下的效果:
二:checkbox的全選 反選操作:
由于這兩個(gè)比較簡單,我就直接上代碼吧:
//全選/取消全選
$('#quanxuan').toggle(function () {
$("input[name='abc']").attr("checked", 'true');
}, function () {
$("input[name='abc']").removeAttr("checked");
});
//反選
$('#fanxuan').click(function () {
$("input[name='abc']").each(function () {
if ($(this).attr("checked")) {
$(this).removeAttr("checked");
} else {
$(this).attr("checked", 'true');
}
});
});
再總結(jié)一下:
jquery版本在1.3之前時(shí),獲取checkbox的選中項(xiàng)的操作:
$("input[name='abc'][checked]").each(function () {
alert(this.value);
});
jquery版本在1.3之后時(shí),獲取checkbox的選中項(xiàng)的操作:
$("input[name='abc']:checked").each(function () {
alert(this.value);
});
附 完整測試Demo代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="js/jquery-1.7.2.min.js"></script>
<script>
$(function () {
//獲取選中項(xiàng)(FF和chrome下無效)
$('#huoqu').click(function () {
//$("input[name='abc'][checked]").each(function () {
// alert(this.value);
//});
$('#show').html("");
$("input[name='abc'][checked]").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
//獲取選中項(xiàng)
$('#huoqu2').click(function () {
$('#show').html("");
$("input[name='abc']:checked").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
//全選/取消全選
$('#quanxuan').toggle(function () {
$("input[name='abc']").attr("checked", 'true');
}, function () {
$("input[name='abc']").removeAttr("checked");
});
//反選
$('#fanxuan').click(function () {
$("input[name='abc']").each(function () {
if ($(this).attr("checked")) {
$(this).removeAttr("checked");
} else {
$(this).attr("checked", 'true');
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="checkbox" name="abc" value="一年級" id="in1" checked="checked" /><label for="in1">一年級</label>
<input type="checkbox" name="abc" value="二年級" id="in2" /><label for="in2">二年級</label>
<input type="checkbox" name="abc" value="三年級" id="in3" /><label for="in3">三年級</label>
<input type="checkbox" name="abc" value="四年級" id="in4" /><label for="in4">四年級</label>
<input type="checkbox" name="abc" value="五年級" id="in5" /><label for="in5">五年級</label>
<input type="checkbox" name="abc" value="六年級" id="in6" /><label for="in6">六年級</label>
<input type="checkbox" name="abc" value="七年級" id="in7" /><label for="in7">七年級</label>
<input type="checkbox" name="abc" value="八年級" id="in8" /><label for="in8">八年級</label>
</div>
<br />
<input type="button" id="huoqu" value="獲取選中項(xiàng)(FF和chrome下無效)" />
<input type="button" id="quanxuan" value="全選/取消全選" />
<input type="button" id="fanxuan" value="反選" />
<input type="button" id="huoqu2" value="獲取選中項(xiàng)" />
<br />
選中項(xiàng): <div id="show">
</div>
</form>
</body>
</html>
作者:酷小孩
- jQuery下拉菜單的實(shí)現(xiàn)代碼
- jQuery實(shí)現(xiàn)的無限級下拉菜單功能示例
- jQuery實(shí)現(xiàn)的導(dǎo)航下拉菜單效果示例
- jQuery實(shí)現(xiàn)的導(dǎo)航下拉菜單效果
- jQuery插件cxSelect多級聯(lián)動下拉菜單實(shí)例解析
- jQuery實(shí)現(xiàn)二級下拉菜單效果
- jquery無限級聯(lián)下拉菜單簡單實(shí)例演示
- 用jquery實(shí)現(xiàn)下拉菜單效果的代碼
- jquery仿京東導(dǎo)航/仿淘寶商城左側(cè)分類導(dǎo)航下拉菜單效果
- jQuery點(diǎn)擊彈出下拉菜單的小例子
- jquery實(shí)現(xiàn)鼠標(biāo)滑過顯示二級下拉菜單效果
- jquery操作復(fù)選框(checkbox)的12個(gè)小技巧總結(jié)
- jQuery實(shí)現(xiàn)的checkbox級聯(lián)選擇下拉菜單效果示例
相關(guān)文章
JQuery記住用戶名和密碼的具體實(shí)現(xiàn)
這篇文章主要介紹了JQuery實(shí)現(xiàn)記住用戶名和密碼的方法,需要的朋友可以參考下2014-04-04基于jQuery的上下無縫滾動應(yīng)用(單行或多行)
基于jQuery的上下無縫滾動應(yīng)用,可應(yīng)用于多行或者單行.詳解請參考注釋.2010-08-08再分享70+免費(fèi)的jquery 圖片滑塊效果插件和教程
這些jQuery插件同樣可以實(shí)現(xiàn)圖片和內(nèi)容的滑塊效果。下面的這些jQuery插件的Demo網(wǎng)站我都測試了一些,都可以正常訪問,我相信那么多款插件中總會有你喜歡的2014-12-12深入學(xué)習(xí)jQuery Validate表單驗(yàn)證(二)
這篇文章主要針對jQuery Validate表單驗(yàn)證為大家進(jìn)行詳細(xì)介紹,通過name屬性來關(guān)聯(lián)字段來驗(yàn)證,改變默認(rèn)的提示信息,將校驗(yàn)規(guī)則寫到j(luò)s代碼中,感興趣的小伙伴們可以參考一下2016-01-01jQuery操作 input type=checkbox的實(shí)現(xiàn)代碼
jQuery操作 input type=checkbox的實(shí)現(xiàn)代碼,需要的朋友可以參考下,這邊腳本之家推薦大家看我們以前發(fā)布的文章2012-06-06