<div class="codetitle"> <span><a style="CURSOR: pointer" data="80236" class="copybut" id="copybut80236" onclick="doCopy('code80236')"><u>复aze代码</u></a></span> 代码如下:</div> <div class="codebody" id="code80236"> <br><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 전환//EN" "http://www.w3.org/TR/html4/loose.dtd"> <br><html> <br><머리> <br><mce:style><!-- <br><br>--></mce:style><style mce_bogus="1"> <br><br></style> <br><title>JS获取复选框被选中的值</title> <br></head> <br><본문> <br><input type="checkbox" name="test" value="0" />0 <br><input type="checkbox" name="test" value="1" />1 <br><input type="checkbox" name="test" value="2" />2 <br><input type="checkbox" name="test" value="3" />3 <br><input type="checkbox" name="test" value="4" />4 <br><input type="checkbox" name="test" value="5" />5 <br><input type="checkbox" name="test" value="6" />6 <br><input type="checkbox" name="test" value="7" />7 <br><input type="button" onclick="chk()" value="提 交" /> <br></body> <br></html <BR></div> <BR>JS代码 <BR><div class="codetitle"><span><a style="CURSOR: pointer" data="71950" class="copybut" id="copybut71950" onclick="doCopy('code71950')"><U>复代码</U></a></span> 代码如下:</div> <div class="codebody" id="code71950"> <BR><mce:script src="jquery.js" mce_src="jquery.js"></mce:script><!--这是载入jquery.js文件,如果不使사용jquery可以去掉--> <br><mce:script type="text/javascript"><!-- <br><br>function chk(){ <BR>var obj=document.getElementsByName('test'); //选择所有name="'test'"적对象,返回数组 <BR>//取到对象数组后,我们来循环检测它是不是被选中 <BR>var s=''; <BR>for(var i=0; i<obj.length; i ){ <BR>if(obj[i].checked) s =obj[i].value ','; //如果选中,将value添加到变weights中 <BR>} <BR>//那么现在来检测s的值就道选中的复选框的值了 <BR>alert(s==''?'你还没有选择任何内容!':s); <BR>} <br><br>function jqchk(){ //jquery获取复选框值 <BR>var chk_value =[]; <BR>$('input[name="test"]:checked').each(function(){ <BR>chk_value.push($(this).val()); <BR>}); <BR>alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value); <BR>} <BR>// --></mce:script> <br> </div> <br>对checkbox의 其他几个操작 <br><br>1. 전체<br>2. 取消全选 <br>3. 选中所有奇数 <br>4. 反选 <br>5. 获得选中的所有值 <br><br>js代码 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="49924" class="copybut" id="copybut49924" onclick="doCopy('code49924')"><u>复代码</u></a></span> 代码如下:</div> <div class="codebody" id="code49924"> <br>$("document").ready(function(){ <br>$("#btn1").click(function(){ <br>$("[name='checkbox']").attr(" 확인됨",'true');//전체 <br>}) <br>$("#btn2").click(function(){ <br>$("[name='checkbox']"). RemoveAttr("checked");//取消全选 <br>}) <br>$("#btn3").click(function(){ <br>$("[name='checkbox']:even" ).attr("checked",'true');//选中所有奇数 <br>}) <br>$("#btn4").click(function(){ <br>$("[name=' checkbox']").each(function(){//反选 <br>if($(this).attr("checked")){ <br>$(this).removeAttr("checked"); <br>} <br>else{ <br>$(this).attr("checked",'true') <br>} <br>}) <br>}) <br>$("#btn5") .click(function(){//출현중적值 <br>var str=""; <br>$("[name='checkbox'][checked]").each(function(){ <br> str =$(this).val() "/r/n"; <br>//alert($(this).val()) <br>alert(str); }) <br>}) <br><br> <br>html代码: </div> <br><br><div class="codetitle"><span>复代码<a style="CURSOR: pointer" data="89445" class="copybut" id="copybut89445" onclick="doCopy('code89445')"><u></u> 代码如下:</a><div class="codebody" id="code89445"> <br><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <br><html xmlns="http://www.w3.org/1999/xhtml"> <br><머리> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <br><제목>louis-blog >> jQuery를 체크박스로 만드는 작업</title> <br><mce:script type='text/javascript' src="http://leotheme.cn/wp-includes/js/jquery/jquery.js" mce_src="http://leotheme.cn/wp -includes/js/jquery/jquery.js"></mce:script> <br><SCRIPT LANGUAGE="자바스크립트"> <br><!-- <BR>$("document").ready(function(){ <BR>$("#btn1").click(function(){ <BR>$("[name= 'checkbox']").attr("checked",'true');//전체 <BR>}) <BR>$("#btn2").click(function(){ <BR>$(" [name='checkbox']").removeAttr("checked");//取消전체选 <BR>}) <BR>$("#btn3").click(function(){ <BR>$(" [name='checkbox']:even").attr("checked",'true');//选中所有奇数 <BR>}) <BR>$("#btn4").click(function(){ <BR>$("[name='checkbox']").each(function(){//反选 <BR>if($(this).attr("checked")){ <BR>$(this) .removeAttr("checked"); <BR>} <BR>else{ <BR>$(this).attr("checked",'true'); <BR>} <BR>}) <BR>}) <BR>$("#btn5").click(function(){//출시 중적 <BR>var str=""; <BR>$("[name='checkbox'][checked]") .each(function(){ <BR>str =$(this).val() "/r/n"; <BR>//alert($(this).val()); <BR>}) <BR>경고(str); <BR>}) <BR>}) <BR>--> <br></SCRIPT> <br></HEAD> <br><body style="text-align:center;margin: 0 auto;font-size: 12px;" mce_style="text-align:center;margin: 0 auto;font-size: 12px;"> <br><div style="border: 1px solid #999; width: 500px; padding: 15px; background: #eee; margin-top: 150px;"> <br><form name="form1" method="post" action=""> <br><input type="button" id="btn1" value="전체选"> <br><input type="button" id="btn2" value="取消全选"> <br><input type="button" id="btn3" value="选中所有奇数"> <br><input type="button" id="btn4" value="反选"> <br><input type="button" id="btn5" value="获得选中的所有值"> <br><br /><br /> <br><input type="checkbox" name="checkbox" value="checkbox1"> <br>checkbox1 <br><input type="checkbox" name="checkbox" value="checkbox2"> <br>checkbox2 <br><input type="checkbox" name="checkbox" value="checkbox3"> <br>checkbox3 <br><input type="checkbox" name="checkbox" value="checkbox4"> <br>checkbox4 <br><input type="checkbox" name="checkbox" value="checkbox5"> <br>checkbox5 <br><input type="checkbox" name="checkbox" value="checkbox6"> <br>체크박스6 <br></form> <br></div> <br></body> <br></HTML> <br> </div></span></div>