<div class="codetitle"> <span><a style="CURSOR: pointer" data="27271" class="copybut" id="copybut27271" onclick="doCopy('code27271')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code27271"> <br> <br> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <br><title>全选</title> <br> <br> <br><div>demo <br><input type="button" onclick="alert(ob.checkedIds());" value="选中ID"> <br><input type="button" onclick="alert(ob.checkedTexts());" value="选中值"> <br><input type="button" onclick="alert(ob.checkedKeys('value1'));" value="选中属性值"> <br> </div> <br><table class="infor"> <br><tr> <br><th> <input name="allcheck" id="allcheck1" type="checkbox" value="1"> <br>全选</th> <br> </tr> <tr> <br><td> <input name="record" type="checkbox" value="1" value1="11">去 <br> </td> </tr> <tr> <br><td> <input name="record" type="checkbox" value="2" value1="22">啊 <br> </td> </tr> <tr> <br><td> <input name="record" type="checkbox" value="3" value1="33">我 <br> </td> </tr> <tr> <br><td> <input name="record" type="checkbox" value="4" value1="44">饿 <br> </td> </tr> <br> </table> <br><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <br><script type="text/javascript"> <BR>//http://www.cnblogs.com/libsource <BR>(function($){ <BR>$.allcheck=function(options){ <BR>_defaults = { <BR>allcheckid:"allcheck", <BR>checkboxname:'record' <BR>}; <BR>o = $.extend(_defaults,options); <BR>_allck=$("#"+o.allcheckid); <BR>_tbl=_allck.parents("table"); <BR>//返回所有选中checkbox的id集合 <BR>checkedIds=function () { <BR>var ids = ""; <BR>$("input[name=" + o.checkboxname + "]").each(function() { <BR>if ($(this).attr("checked")) <BR>ids += $(this).val() + ","; <BR>}); <BR>return ids.replace(/,$/,''); <BR>} <BR>//返回所有选中checkbox的key属性集合 <BR>checkedKeys=function (key) { <BR>var ids = ""; <BR>$("input[name=" + o.checkboxname + "]").each(function() { <BR>if ($(this).attr("checked")) <BR>ids += $(this).attr(key) + ","; <BR>}); <BR>return ids.replace(/,$/,''); <BR>} <BR>//返回所有选中checkbox的文本集合 <BR>checkedTexts=function () { <BR>var txts = ""; <BR>$("input[name=" + o.checkboxname + "]").each(function() { <BR>if ($(this).attr("checked")) <BR>txts += gtrim($(this).parent().text()) + ","; <BR>}); <BR>return txts.replace(/,$/,''); <BR>} <BR>gtrim=function (txt) { <BR>return txt.replace(/(^\s*)|(\s*$)/g, ""); <BR>} <BR>//设置所有选中checkbox的id集合 <BR>setCheckedIds=function (checkids) { <BR>checkids = ","+checkids+","; <BR>$("input[name=" + o.checkboxname + "]").each(function() { <BR>if (checkids.match(","+$(this).val()+",")) <BR>$(this).attr("checked","checked"); <BR>}); <BR>} <BR>//检查所有checkbox是否全选 <BR>_checkAll=function () { <BR>if (this.checked && $("input:checkbox:not([checked]):not(#" + o.allcheckid + ")", _tbl).length == 0) <BR>_allck[0].checked = true; <BR>else <BR>_allck[0].checked = false; <BR>} <br><br>//全选checkbox状态 <BR>_setAllChecked=function () { <BR>if (!this.checked) <BR>$("input:checkbox", _tbl).removeAttr("checked"); <BR>else <BR>$("input:checkbox", _tbl).not(this).attr("checked", "checked"); <BR>} <BR>_allck.click(_setAllChecked); <BR>$("input:checkbox[name="+o.checkboxname+"]").each(function(){$(this).click(_checkAll);}); <BR>return {checkedIds:checkedIds,checkedKeys:checkedKeys,checkedTexts:checkedTexts,setCheckedIds:setCheckedIds}; <BR>}; <BR>})(jQuery); <BR></script> <br><script type="text/javascript"> <BR>var ob=$.allcheck({allcheckid:'allcheck1'}); <BR>//设置选项allcheckid checkboxname <BR>//取返回值可以调用checkedIds,checkedKeys,checkedTexts <BR></script> <br> <br><br> </div>