jQueryは簡単な検証コード機能を実現する

2981 ワード

jQueryを学ぶ過程で、簡単な検証コードの小さな例を書いて、記載して、便利に後で参考にして補充して、ソースコードは以下の通りです:

 
 
 
   
  <style type="text/css"> 
  div{ 
    background-color:blue; 
    width:200px; 
    height:100px; 
    font-size:35px; 
  } 
  </style> 
  <script src="../jquery-1.8.0.js" type="text/javascript"/> 
  <script type="text/javascript"> 
    $(document).ready(function() { 
       //       
      //    
      var code; 
      function createCode(){ 
        code = '';//    code      
        var codeLength = 4;//    ,     ,      4 
        var codeV = $("div"); 
        //       
        var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z'); 
        for(var i = 0; i < codeLength; i++){ //  codeLength     4    4    
           var index = Math.floor(Math.random()*36); //       ,    0 ~ 36  
           code += random[index]; //                    
      } 
        codeV.text(code);//              Value 
      } 
      //          
      createCode(); 
      //   Div       
      $("div").bind('click',function() { 
          createCode(); 
        }); 
      //        ==   ,     
      $("#b1").bind('click',function() { 
         var oValue = $("#in1").val().toUpperCase(); 
         $("#l1").html(""); 
        if(oValue ==""){ 
          $("#l1").html("<font color='red'>      </font>"); 
        }else if(oValue != code){ 
          $("#l1").html("<font color='red'>      ,     </font>"); 
          oValue = ""; 
          createCode(); 
        }else{ 
          $("#l1").html("<font color='blue'>     </font>"); 
        } 
      });  
    }); 
  </script> 
 
 
<center> 
<label>      :</label><input type="text" id="in1" value="" placeholder="      "/> 
<button id="b1">    </button> 
  <div/><label id="l1"/> 
</center> 
 
 </code></pre> 
 </div> 
  
 <div class="clearfix"> 
  <span id="art_bot" class="jbTestPos"/> 
 </div> 
</div>
                            </div>
                        </div>