购物车的前台管理程序-只与服务器交互一次(二)

网络整理 - 08-18
cart.js

请存为cart.js,并且和前部分html放在同一个目录中

//------------------------------------------------------------------------------
//
// file name :       cart.js
//
// description:      购物车前台用javascript 函数库
//
// function list :   On_Cancel(txtAmount , txtSum)---- 取消该项
//                   On_Update()                  ---- 更新数量
//                   On_Del()                     ---- 全部删除
//                   UpdateDate()                 ---- 更新表单显示
//                   Round()                      ---- 四舍五入取指定小数位数
//
// Aurthor:          LiuYunpeng
//
// Date:             2000/09/03
//
// History:          2000/09/03 version 1.0
//
//------------------------------------------------------------------------------


//----------------code begin here------------------------------------------

  function On_Cancel(txtAmount , txtSum)
    {
      txtAmount.value = 0 ;
      txtSum.value = 0 ;
      UpdateData();      
    }

  //更新表单显示
  function UpdateData()
    {
      var m_aAmount   = new Array() ;
      var m_aPrice    = new Array() ;
      var m_aDiscount = new Array() ;
      var m_aSum      = new Array() ;
      var m_numTotal  = 0 ;
      var i , j , k  , m;
      i = 0 ;
      j = 0 ;
      k = 0 ;
      m = 0 ;
      
      //求数量
      for (i = 0 ; i < document.frmOrder .elements .length ; i ++ )
        {
           if (frmOrder.elements [i].name.substring(0 , 9) == "txtAmount")
             {
               m_aAmount[j] = frmOrder.elements [i].value ;
               j ++ ;
             }
           if (frmOrder.elements [i].name.substring(0 , 14) == "txtTicketPrice")
             {
              
              m_aPrice[k] = Round(frmOrder.elements[i].value , 2) ;
              k ++ ;
             }
           if (frmOrder.elements [i].name.substring(0 , 14) == "hidTicketDisco")
             {
              m_aDiscount[m] = frmOrder.elements[i].value ;
              m ++ ;
             }
         }
         
      for ( i = 0 ; i < k ; i ++)
         {
           m_aSum[i] = Round(m_aPrice[i] * m_aAmount[i] * m_aDiscount[i] / 100 , 2);
           
         }
                 
                  
      //更新form
      j = 0 ;
      for (i = 0 ; i < document.frmOrder .elements .length ; i ++ )
        {
           if (frmOrder.elements [i].name.substring(0 , 6) == "txtSum")
             {
               frmOrder.elements [i].value = m_aSum[j] ;
               m_numTotal = Round(m_numTotal + m_aSum[j] , 2);
               j ++ ;
             }
             
         }
       
      document.frmOrder .txtTotal .value = m_numTotal ;
         
      return 0;              
   }       

  //更新数量
  function On_Update()
    {
    
      if (document.frmOrder .btnUpdate .value == "修改数量")
         {
          document.frmOrder .btnUpdate .value = "更新数量"
          for (var i=0 ; i < frmOrder.elements.length ; i++)
             {
               if (frmOrder.elements [i].name.substring(0 , 9) == "txtAmount")
                 {

                   frmOrder.elements [i].disabled = false ;
                   frmOrder.elements[i].style.borderStyle   = "ridge ";
                   document.frmOrder .btnBuy .disabled      = true ;
                   document.frmOrder .btnCancel .disabled   = true ;
                   document.frmOrder .btnContinue .disabled = true ;
                   document.frmOrder .btnDel .disabled      =true ;
                 }  
              
              }
          }
      else
         {
          //alert(document.frmOrder .btnUpdate .value) ;
          document.frmOrder .btnUpdate .value = "修改数量"
          var j = 0 ;
          var m_aPrice = new Array();
          var m_aAmount = new Array() ;
          for (var i=0 ; i < frmOrder.elements.length ; i++)
             {
               if (frmOrder.elements [i].name.substring(0 , 9) == "txtAmount")
                 {

                   frmOrder.elements [i].disabled           = true ;
                   frmOrder.elements[i].style.borderStyle   = "none";
                   document.frmOrder .btnBuy .disabled      = false ;
                   document.frmOrder .btnCancel .disabled   = false ;
                   document.frmOrder .btnContinue .disabled = false ;
                   document.frmOrder .btnDel .disabled      = false ;
                 }
              }
          }
                 
      UpdateData() ;  
    }       

//全部删除
function On_Del()
    {
     for (var i=0 ; i < frmOrder.elements.length ; i++)
        {
         if (frmOrder.elements [i].name.substring(0 , 9) == "txtAmount")
           {
            frmOrder.elements [i].value = 0 ;
           }
        }
      
     UpdateData();
    }   
   
   //继续购物
   function On_Continue()
     {
      document.frmOrder.submit();
     }
   
   //区收银台
   function On_Buy()
     {
      document.frmOrder.submit();
     }
   
   //四舍五入,保留制定小数位数     
   function Round(a_Num , a_Bit)
    {
      return( Math.round(a_Num * Math.pow (10 , a_Bit)) / Math.pow(10 , a_Bit))  ;
    }          
    
//---------------------code end-------------------------------------------------------


原作者:不详
来 源:不详