一天一個js(3)多選框簡單操作

來源:互聯網
上載者:User

[html] 
<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(function(){ 
    $(".put1").click(function(){ 
        $(":checkbox").attr("checked","checked");    
    }); 
    $(".put2").click(function(){ 
            $(":checkbox").each(function(){ 
                if($(this).is(":checked")){ 
                    $(this).removeAttr("checked"); 
                }else{ 
                    $(this).attr("checked","checked"); 
                }; 
            }); 
    }); 
    $(".put3").click(function(){ 
        var count = $(":checkbox:checked").length; 
        var sum = ""; 
        $(":checkbox:checked").each(function(){ 
            var array = $(this).siblings("label").text(); 
            for(i=0;i<array.length;i++){ 
            sum = sum+array[i]; 
            }; 
        }); 
        $("h4").text(sum).siblings("h3").text("您總共選擇了"+count+"個,分別是:"); 
    }); 
}); 
</script> 
<style type="text/css"> 
div { width:800px; padding:10px; border:1px solid #000; margin:200px auto;} 
h3 { color:#900;} 
</style> 
<title>多選框簡單操作</title> 
</head> 
 
<body> 
<div> 
<p><label for="c1">1.蘋果</label><input id="c1" type="checkbox" /></p> 
<p><label for="c2">2.香蕉</label><input id="c2" type="checkbox" /></p> 
<p><label for="c3">3.橘子</label><input id="c3" type="checkbox" /></p> 
<p><label for="c4">4.菠蘿</label><input id="c4" type="checkbox" /></p> 
<p><label for="c5">5.西瓜</label><input id="c5" type="checkbox" /></p> 
<p><label for="c6">6.橙子</label><input id="c6" type="checkbox" /></p> 
<p><label for="c7">7.鴨梨</label><input id="c7" type="checkbox" /></p> 
<p><label for="c8">8.柚子</label><input id="c8" type="checkbox" /></p> 
<p><label for="c9">9.葡萄</label><input id="c9" type="checkbox" /></p> 
<p><input type="button" class="put1" value="全選" /> 
<input type="button" class="put2" value="反選" /> 
<input type="button" class="put3" value="顯示我選擇的" /></p> 
<h3></h3><h4></h4> 
</div> 
</body> 
</html> 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
 $(".put1").click(function(){
  $(":checkbox").attr("checked","checked"); 
 });
 $(".put2").click(function(){
   $(":checkbox").each(function(){
    if($(this).is(":checked")){
     $(this).removeAttr("checked");
    }else{
     $(this).attr("checked","checked");
    };
   });
 });
 $(".put3").click(function(){
  var count = $(":checkbox:checked").length;
  var sum = "";
  $(":checkbox:checked").each(function(){
   var array = $(this).siblings("label").text();
   for(i=0;i<array.length;i++){
   sum = sum+array[i];
   };
  });
  $("h4").text(sum).siblings("h3").text("您總共選擇了"+count+"個,分別是:");
 });
});
</script>
<style type="text/css">
div { width:800px; padding:10px; border:1px solid #000; margin:200px auto;}
h3 { color:#900;}
</style>
<title>多選框簡單操作</title>
</head>

<body>
<div>
<p><label for="c1">1.蘋果</label><input id="c1" type="checkbox" /></p>
<p><label for="c2">2.香蕉</label><input id="c2" type="checkbox" /></p>
<p><label for="c3">3.橘子</label><input id="c3" type="checkbox" /></p>
<p><label for="c4">4.菠蘿</label><input id="c4" type="checkbox" /></p>
<p><label for="c5">5.西瓜</label><input id="c5" type="checkbox" /></p>
<p><label for="c6">6.橙子</label><input id="c6" type="checkbox" /></p>
<p><label for="c7">7.鴨梨</label><input id="c7" type="checkbox" /></p>
<p><label for="c8">8.柚子</label><input id="c8" type="checkbox" /></p>
<p><label for="c9">9.葡萄</label><input id="c9" type="checkbox" /></p>
<p><input type="button" class="put1" value="全選" />
<input type="button" class="put2" value="反選" />
<input type="button" class="put3" value="顯示我選擇的" /></p>
<h3></h3><h4></h4>
</div>
</body>
</html>


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.