jquery操作checkbox的常用方法總結(jié)【附測試源碼下載】
本文實例講述了jquery操作checkbox的常用方法。分享給大家供大家參考,具體如下:
做系統(tǒng)的過程中,與checkbox 復(fù)選框打交道,是難免的,而且在每個系統(tǒng)中,簡直是必不可少的一道菜,通常的操作有一下幾種:
1.用jquery 全部選擇checkbox
2.用jquery 全部取消checkbox
3.用jquery 實現(xiàn)checkbox 反選
4.用jquery 獲取所有已選擇checkbox的值。
現(xiàn)在將這些常用的操作總結(jié)在一個例子中,方便以后查詢,不愿意看代碼的,也可以下載源碼運行測試。
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>yihaomen.com jquery checkbox 測試</title>
<link rel="stylesheet" type="text/css" href="/static/css/global.css" rel="external nofollow" >
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
/*全選或全不選*/
$("#controlall").click(function() {
$('input[name="selectdivision"]').attr("checked",this.checked);
});
var $subBox = $("input[name='selectdivision']");
$subBox.click(function(){
$("#controlall").attr("checked",$subBox.length == $("input[name='selectdivision']:checked").length ? true : false);
});
/*反選*/
var reverseBtn=$('#reverseselect');
reverseBtn.click(function(){
$("[name='selectdivision']").each(function(){
if($(this).attr("checked"))
{
$(this).removeAttr("checked");
}
else
{
$(this).attr("checked",'true');
}
})
});
/*得到所有值*/
var getvalueBtn=$('#getValButton');
getvalueBtn.click(function(){
var val_array=[];
$("input:checkbox[name=selectdivision]:checked").each(function(){
val_array.push(parseInt($(this).val()));
});
alert(val_array.join());
});
});
</script>
</head>
<body>
<div>
<table style="border:1px dashed #ccc;margin-top:5px;" class="crmgrid_popwindow" id="division_table">
<tr style="background-color:#F9F9F9;">
<td><input type="checkbox" id="controlall" name="controlall" />全選/全不選</td>
<td>
<input type="button" id="reverseselect" value="反選" />
<input type="button" id="getValButton" value="得到選擇的值" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="1"/></td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="2"/></td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="3"/></td>
<td>3</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="4"/></td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="5"/></td>
<td>5</td>
</tr>
<tr>
<td><input type="checkbox" name="selectdivision" value="6"/></td>
<td>6</td>
</tr>
</table>
</div>
</body>
</html>
源碼點擊此處本站下載。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結(jié)》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
基于jQuery實現(xiàn)圖片推拉門動畫效果的兩種方法
本文給大家分享兩種方法實現(xiàn)''推拉門''動畫效果也可以稱作是手風(fēng)琴效果,具體實現(xiàn)方法大家通過本文一起學(xué)習(xí)吧2017-08-08
jQuery時間驗證和轉(zhuǎn)換為標(biāo)準(zhǔn)格式的時間格式
本篇文章主要介紹了jQuery時間驗證和轉(zhuǎn)換為標(biāo)準(zhǔn)格式的時間示例代碼,具有一定的參考價值,有興趣的可以了解一下。2017-03-03
推薦40款強大的 jQuery 導(dǎo)航插件和教程(上篇)
在下面的集合中,你會發(fā)現(xiàn)很多便利的 jQuery 導(dǎo)航插件和有用的教程,幫助你實現(xiàn)充滿吸引力的網(wǎng)站導(dǎo)航,讓你網(wǎng)站更有組織性和交互性2012-09-09

