本文執行個體介紹了javascript實現checkbox複選框執行個體代碼以及對checkbox複選框進行美化操作,分享給大家供大家參考,具體內容如下
1、checkbox複選框進行美化操作
複選框預設外表的美觀度差強人意,能夠滿足美觀度要求不高的頁面,但是如果對於頁面要求較為精緻,那可能就過於勉強了,下面就一段對複選框進行美化的代碼執行個體,希望能夠給大家帶來一定的協助。
代碼執行個體如下:
<!DOCTYPE html><html><head><meta charset=" utf-8"><title>checkbox複選框</title><style type="text/css">.CheckBoxClass{display:none;}.CheckBoxLabelClass{ background:url("mytest/jQuery/UnCheck.png") no-repeat; padding-left:30px; padding-top:3px; margin:5px; height:28px; width:150px; display:block;}.CheckBoxLabelClass:hover{text-decoration:underline;}.LabelSelected{background:url("mytest/jQuery/Check.png") no-repeat;}</style><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script><script type="text/javascript">$(document).ready(function(){ $(".CheckBoxClass").change(function(){ if($(this).is(":checked")){ $(this).next("label").addClass("LabelSelected"); } else{ $(this).next("label").removeClass("LabelSelected"); } });})</script></head><body><div> <input id="CheckBox1" type="checkbox" class="CheckBoxClass"/> <label id="Label1" for="CheckBox1" class="CheckBoxLabelClass">雲棲社區一</label> <input id="CheckBox2" type="checkbox" class="CheckBoxClass"/> <label id="Label2" for="CheckBox2" class="CheckBoxLabelClass">雲棲社區二</label></div></body></html>
2、checkbox複選框全選和取消全選執行個體代碼
關於複選框的全選和不全選是非常常見的操作,本章節分享一段代碼執行個體,能夠實現多種選中和取消選中功能。
代碼執行個體如下:
<!DOCTYPE html><html><head><meta charset=" utf-8"><title>雲棲社區</title><script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> <script type="text/javascript">$("document").ready(function(){ $("#all").click(function(){ if(this.checked) { $("input[name='checkbox']").each(function(){this.checked=true;}); $("#btn1").attr("value","反選"); } else { $("input[name='checkbox']").each(function(){this.checked=false;}); $("#btn1").attr("value","全選"); } }); $("#btn1").click(function(){ $("[name='checkbox']").attr("checked",'true'); }) $("#cancel").click(function(){ $("[name='checkbox']").removeAttr("checked"); }) $("#jishu").click(function(){ $("[name='checkbox']:even").attr("checked",'true'); }) $("#fanxuan").click(function(){ $("[name='checkbox']").each(function(){ if($(this).attr("checked")) { $(this).removeAttr("checked"); } else { $(this).attr("checked",'true'); } }) }) $("#get").click(function(){ var str=""; $("[name='checkbox'][checked]").each(function(){ str+=$(this).val()+"/r/n"; }) alert(str); })}) </script> </head> <body> <form name="form1" method="post" action=""> <input type="checkbox" id="all" name="all"> <input type="button" id="btn1" value="全選"> <input type="button" id="cancel" value="取消全選"> <input type="button" id="jishu" value="選中所有奇數"> <input type="button" id="fanxuan" value="反選"> <input type="button" id="get" value="獲得選中的所有值"> <br> <input type="checkbox" name="checkbox" value="雲棲社區一"> 雲棲社區一 <input type="checkbox" name="checkbox" value="雲棲社區二"> 雲棲社區二 <input type="checkbox" name="checkbox" value="雲棲社區三"> 雲棲社區三 <input type="checkbox" name="checkbox" value="雲棲社區四"> 雲棲社區四 <input type="checkbox" name="checkbox" value="雲棲社區五"> 雲棲社區五 <input type="checkbox" name="checkbox" value="雲棲社區六"> 雲棲社區六 <input type="checkbox" name="checkbox" value="雲棲社區七"> 雲棲社區七 <input type="checkbox" name="checkbox" value="雲棲社區八"> 雲棲社區八 </form></body></html>
以上就是關於checkbox複選框的全部內容,希望對大家的學習有所協助。