jquery 頁面全選框?qū)嵺`代碼
更新時(shí)間:2010年04月02日 22:25:11 作者:
頁面中經(jīng)常遇到與全選相關(guān)的操作,利用jquery集成了一下,主要為了方便使用吧~
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>全選</title>
</head>
<body>
<div>demo
<input type="button" onclick="alert(ob.checkedIds());" value="選中ID"/>
<input type="button" onclick="alert(ob.checkedTexts());" value="選中值"/>
<input type="button" onclick="alert(ob.checkedKeys('value1'));" value="選中屬性值"/>
</div>
<table class="infor">
<tr>
<th><input name="allcheck" id="allcheck1" type="checkbox" value="1"/>
全選</th>
</tr><tr>
<td><input name="record" type="checkbox" value="1" value1="11"/>去
</td></tr><tr>
<td><input name="record" type="checkbox" value="2" value1="22"/>啊
</td></tr><tr>
<td><input name="record" type="checkbox" value="3" value1="33"/>我
</td></tr><tr>
<td><input name="record" type="checkbox" value="4" value1="44"/>餓
</td></tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" >
//http://www.cnblogs.com/libsource
(function($){
$.allcheck=function(options){
_defaults = {
allcheckid:"allcheck",
checkboxname:'record'
};
o = $.extend(_defaults,options);
_allck=$("#"+o.allcheckid);
_tbl=_allck.parents("table");
//返回所有選中checkbox的id集合
checkedIds=function () {
var ids = "";
$("input[name=" + o.checkboxname + "]").each(function() {
if ($(this).attr("checked"))
ids += $(this).val() + ",";
});
return ids.replace(/,$/,'');
}
//返回所有選中checkbox的key屬性集合
checkedKeys=function (key) {
var ids = "";
$("input[name=" + o.checkboxname + "]").each(function() {
if ($(this).attr("checked"))
ids += $(this).attr(key) + ",";
});
return ids.replace(/,$/,'');
}
//返回所有選中checkbox的文本集合
checkedTexts=function () {
var txts = "";
$("input[name=" + o.checkboxname + "]").each(function() {
if ($(this).attr("checked"))
txts += gtrim($(this).parent().text()) + ",";
});
return txts.replace(/,$/,'');
}
gtrim=function (txt) {
return txt.replace(/(^\s*)|(\s*$)/g, "");
}
//設(shè)置所有選中checkbox的id集合
setCheckedIds=function (checkids) {
checkids = ","+checkids+",";
$("input[name=" + o.checkboxname + "]").each(function() {
if (checkids.match(","+$(this).val()+","))
$(this).attr("checked","checked");
});
}
//檢查所有checkbox是否全選
_checkAll=function () {
if (this.checked && $("input:checkbox:not([checked]):not(#" + o.allcheckid + ")", _tbl).length == 0)
_allck[0].checked = true;
else
_allck[0].checked = false;
}
//全選checkbox狀態(tài)
_setAllChecked=function () {
if (!this.checked)
$("input:checkbox", _tbl).removeAttr("checked");
else
$("input:checkbox", _tbl).not(this).attr("checked", "checked");
}
_allck.click(_setAllChecked);
$("input:checkbox[name="+o.checkboxname+"]").each(function(){$(this).click(_checkAll);});
return {checkedIds:checkedIds,checkedKeys:checkedKeys,checkedTexts:checkedTexts,setCheckedIds:setCheckedIds};
};
})(jQuery);
</script>
<script type="text/javascript">
var ob=$.allcheck({allcheckid:'allcheck1'});
//設(shè)置選項(xiàng)allcheckid checkboxname
//取返回值可以調(diào)用checkedIds,checkedKeys,checkedTexts
</script>
</body>
</html>
您可能感興趣的文章:
- jquery實(shí)現(xiàn)全選、反選、獲得所有選中的checkbox
- JQUERY復(fù)選框CHECKBOX全選,取消全選
- jQuery CheckBox全選、全不選實(shí)現(xiàn)代碼小結(jié)
- 用JQuery實(shí)現(xiàn)全選與取消的兩種簡(jiǎn)單方法
- jQuery判斷checkbox(復(fù)選框)是否被選中以及全選、反選實(shí)現(xiàn)代碼
- jquery操作checkbox實(shí)現(xiàn)全選和取消全選
- jquery復(fù)選框CHECKBOX全選、反選
- JQUERY CHECKBOX全選,取消全選,反選方法三
- jquery一鍵控制checkbox全選、反選或全不選
- jQuery實(shí)現(xiàn)簡(jiǎn)單全選框
相關(guān)文章
jquery UI Datepicker時(shí)間控件的使用及問題解決
這篇文章主要介紹了jquery UI Datepicker時(shí)間控件的使用及與asp.net中的UpdatePanel聯(lián)合使用時(shí)的失效問題解決,感興趣的小伙伴們可以參考一下2016-04-04Jquery Ajax的Get方式時(shí)需要注意URL地方
我們要時(shí)刻注意瀏覽器緩存, 當(dāng)使用GET方式時(shí)要添加時(shí)間戳參數(shù) (net Date()).getTime() 來保證每次發(fā)送的URL不同, 可以避免瀏覽器緩存.2011-04-04jQuery取得元素標(biāo)簽名稱小結(jié)(附代碼)
這篇文章主要介紹了 jquery如何取得元素標(biāo)簽名稱,將html和js代碼附上,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08jQuery實(shí)現(xiàn)滑動(dòng)開關(guān)效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)滑動(dòng)開關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08