jQuery中獲取checkbox選中項等操作及注意事項
2. checkbox選項的全選 反選操作
用于測試的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>
一:首先來說第一點,獲取checkbox的選中項。網(wǎng)上搜到的大部分方法使用each來獲?。?/P>
$("input[name='checkbox'][checked]").each(function () {
alert(this.value);
})
IE下的測試效果(我的是IE10):

IE10下的效果:

chrome瀏覽器下的效果:

通過在google上搜索,找到了原因:

網(wǎng)址: Jquery 選中多少個input CheckBox問題,IE正常,F(xiàn)F和Chrome無法取到值

因為我用的jquery版本是1.7.2的,所以這里我得用 :checked 來獲取才行,修改后的代碼:
//獲取選中項
$('#huoqu2').click(function () {
$('#show').html("");
$("input[name='abc']:checked").each(function () {
//alert(this.value);
$('#show').append(this.value + " ");
});
});
在chrome下的效果:

二:checkbox的全選 反選操作:
由于這兩個比較簡單,我就直接上代碼吧:
//全選/取消全選
$('#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之前時,獲取checkbox的選中項的操作:
$("input[name='abc'][checked]").each(function () {
alert(this.value);
});
jquery版本在1.3之后時,獲取checkbox的選中項的操作:
$("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 () {
//獲取選中項(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 + " ");
});
});
//獲取選中項
$('#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="獲取選中項(FF和chrome下無效)" />
<input type="button" id="quanxuan" value="全選/取消全選" />
<input type="button" id="fanxuan" value="反選" />
<input type="button" id="huoqu2" value="獲取選中項" />
<br />
選中項: <div id="show">
</div>
</form>
</body>
</html>
作者:酷小孩
- jQuery下拉菜單的實現(xiàn)代碼
- jQuery實現(xiàn)的無限級下拉菜單功能示例
- jQuery實現(xiàn)的導航下拉菜單效果示例
- jQuery實現(xiàn)的導航下拉菜單效果
- jQuery插件cxSelect多級聯(lián)動下拉菜單實例解析
- jQuery實現(xiàn)二級下拉菜單效果
- jquery無限級聯(lián)下拉菜單簡單實例演示
- 用jquery實現(xiàn)下拉菜單效果的代碼
- jquery仿京東導航/仿淘寶商城左側(cè)分類導航下拉菜單效果
- jQuery點擊彈出下拉菜單的小例子
- jquery實現(xiàn)鼠標滑過顯示二級下拉菜單效果
- jquery操作復選框(checkbox)的12個小技巧總結(jié)
- jQuery實現(xiàn)的checkbox級聯(lián)選擇下拉菜單效果示例
相關(guān)文章
jQuery操作 input type=checkbox的實現(xiàn)代碼
jQuery操作 input type=checkbox的實現(xiàn)代碼,需要的朋友可以參考下,這邊腳本之家推薦大家看我們以前發(fā)布的文章2012-06-06

