javascript實現checkbox複選框執行個體代碼_javascript技巧

來源:互聯網
上載者:User

本文執行個體介紹了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複選框的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.