欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery多選框選擇數(shù)量限制方法

 更新時間:2017年02月08日 14:09:32   作者:每天都進步一點點  
這篇文章主要為大家詳細介紹了jQuery多選框選擇數(shù)量限制方法,最多選擇 三項 的上限已滿, 其他選項將會變?yōu)椴豢蛇x

在網(wǎng)上找到的,可以使用。由于刷新后,多選框會保留選中的,這時數(shù)量達到要求后還是可以選擇的bug,在前面加個判斷數(shù)量是否數(shù)量達到要求的處理就可以了。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script>
$(function(){

//加上判斷是否達到數(shù)量要求
  if($(":checkbox").size()>=3){
     $(":checkbox").removeAttr("checked");
     $(":checkbox").attr("disabled","disabled");
     $(":checkbox").removeAttr("disabled");
   }

  var num = 0;
  $(":checkbox").each(function(){
    $(this).click(function(){
      if($(this)[0].checked) {
        ++num;
        if(num == 3) {
          //alert("最多選擇 三項 的上限已滿, 其他選項將會變?yōu)椴豢蛇x.");
          $(":checkbox").each(function(){
            if(!$(this)[0].checked) {
              $(this).attr("disabled", "disabled");
            }
          });
        }
      } else {
        --num;
        if(num <= 2) {
          $(":checkbox").each(function(){
            if(!$(this)[0].checked) {
              $(this).removeAttr("disabled");
            }
          });
        }
      }
    });
  });
})
</script> 
</head>

<body>
<input type="checkbox" />上網(wǎng)<br />
<input type="checkbox" />旅游<br />
<input type="checkbox" />逛街<br />
<input type="checkbox" />游戲<br />
<input type="checkbox" />聽歌<br />
<input type="checkbox" />購物<br />
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論