jquery 页面全选框实践代码

2016-02-19 13:32 13 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐jquery 页面全选框实践代码,无聊中的都看过来。

【 tulaoshi.com - Web开发 】

代码如下:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)
html
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
title全选/title
/head
body
divdemo
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
tr
thinput name="allcheck" type="checkbox" value="1"/
全选/th
/trtr
tdinput name="record" type="checkbox" value="1" value1="11"/去
/td/trtr
tdinput name="record" type="checkbox" value="2" value1="22"/啊
/td/trtr
tdinput name="record" type="checkbox" value="3" value1="33"/我
/td/trtr
tdinput 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, "");
}
//设置所有选中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状态
_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'});
//设置选项allcheckid checkboxname
//取返回值可以调用checkedIds,checkedKeys,checkedTexts
/script
/body
/html

来源:https://www.tulaoshi.com/n/20160219/1604597.html

延伸阅读
标签: Web开发
input type="checkbox" id="cr" /label for="cr"我已经阅读/label a 用DOM方式来判断是否被选中 代码如下: $(function(){ var $cr = $("#cr"); var cr =$cr[0]; $cr.click(function(){ if(cr.checked){ alert("谢谢注册"); } else{ alert("NO"); } }); });
标签: Web开发
代码如下: style BODY {FONT-SIZE: 9pt;} table {FONT-SIZE: 9pt;} /style input type=checkbox id=checkall onclick=check_all()label for=checkall全部选择/label 线程数:input type=text id=the_thread value=10 size=2 maxlength=2 style="height:...
标签: Web开发
上图展示了传统 Wordpress 模板在浏览器中的载入顺序,Loading 条的出现和消失分布于头尾。 如果我们在页面的不同位置放置多个 JS ,每个 JS 用于逐步增加 Loading 条的宽度,那么这个 Loading 条无疑会更具实用价值。它在一定程度上缓解了访客等待载入的枯燥感,同时还能客观反映页面载入的进度。若再配以 jQuery 内建的动画效果,其完...
标签: Web开发
jquery.checkboxes.zip,然后解压引用到使用插件的页面, 这个不用多说了吧!下面看具体例子,为了让大家更好的理解,我直接把实现功能的代码贴出来: 代码如下: $("#myform").toggleCheckboxes()//全选,取消全选,反选 $("#myform").toggleCheckboxes(":not(#checkbox1)")//全选,取消全选且不选中第一个,反选 $("#myform").toggleCheckboxes(".to...
标签: Web开发
有很多人不知道在多选框中全选怎么还编写代码。其实很简单,下面就是代码以及说明,请仔细阅读。 以下是引用片段: script language="javascript" //代码说明(lulu163.com):form1为表单名,memberId为复选框,selectbutton为全选按钮    function selectAll()     { for (var i=0;idocument.form1.memberI...

经验教程

573

收藏

10

精华推荐

微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部