2011年1月12日 星期三

Jquery樂透彩選號程式

<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
$(function(){
 numberTable = new Array("1","2","3","4","5","6","7","8","9","10",
       "11","12","13","14","15","16","17","18","19","20",
       "21","22","23","24","25","26","27","28","29","30",
       "31","32","33","34","35","36","37","38","39");
 var nums = $('#character :input');
 //將按鈕的值放入
 nums.each(function(k){
  $(this).val( numberTable[k] );
 });
 //挷定按鈕的事件
 nums.bind('click',function(){  
  var choVal = $('#choice').val();   
  if(choVal!= ''){
   splitArr = choVal.split(",");
   //禁止重覆選號
   for(i=0;i<splitArr.length;i++){
    if( $(this).val() == splitArr[i] ){    
     return false;
    }
   }
   //禁止超過5個選號
   if(splitArr.length == 5){return false;}
  }
 
  
  if( choVal == ''){
   choVal = $(this).val();
  }else{
   choVal += "," + $(this).val();
  }
  $('#choice').val( choVal );
 });
 
 //自動選號 
 $('#auto').bind('click',function(){
  randomCho(5,39);
 });
 //送出檢查
 $('#send').bind('click',function(){
  var choVal = $('#choice').val(); 
  splitArr = choVal.split(",");
  //檢查數量
  if(splitArr.length != 5){
   alert("選號不足5個!");
   return false;
  }else{
   //排序
   splitArr.sort(myUpSort);
   //加逗號
   popStr = splitArr.join(","); 
     
   if(confirm("親愛的玩家您好:您選的號碼是 "+popStr+" 確認選號確認後,我們將於您的帳戶中扣取10,000點紅利 。") ){
  
   $('#form1').submit();
   }else{
   /* location.reload(); */
   }
  }
 });
 function randomCho(total,maxNum){
  var numberTable = new Array("1","2","3","4","5","6","7","8","9","10",
       "11","12","13","14","15","16","17","18","19","20",
       "21","22","23","24","25","26","27","28","29","30",
       "31","32","33","34","35","36","37","38","39");
  var numArray = new Array();
  var reStr = "";
  for(var i=0;i<total;i++){
   var index = Math.floor(Math.random()*(maxNum-1)); //亂數產生0~38之間的數字
   numArray.push( numberTable[index] );   
   //將選到的數字從陣列中刪掉,避免重覆…
   numberTable.splice(index,1);
   maxNum--;
  }
  //排序
  numArray.sort(myUpSort);
  //加逗號
  reStr = numArray.join(",");
  //console.log( reStr );
  $('#choice').val(reStr)  
 }
 //遞增排序
 function myUpSort(x,y){return x-y}
});
</script> 

1 則留言: