2012-03-27 127 views
1

一个函数,在更新任何输入字段时使用jquery对表进行计算。它工作正常,但我需要能够调用它时,我加载数据更新特定的行。我似乎无法正常工作,无论它更新时,输入字段的变化,它不会工作,当我打电话来更新特定的行。Jquery将参数传递给函数无法正常工作

我试图传递一个参数到函数中,告诉哪些行更新,当我需要它,当它检测到一个变化事件时,它检查变量是否被传入。我试图检查传入的变量是未定义或null,但我似乎无法开始工作。我究竟做错了什么?

我的代码;

所以一个调用来更新某一行;

//////////////////////////////////////////// 
//load prices and pid from range selected 
//////////////////////////////////////////// 

$(document).ready(function(){ 
    $("#range_select").change(function(event){ 

    //get the range id 
    $id=$("#range_select").val(); 

    var i; 
    var loadedValues; 
    var result; 
    var pid; 

    loadedValues=0; 

    //clear All the prices if loaded, reset background color 
    $(".price").val(0); 
    $(".price").css("background-color","#FFF"); 

    //clear ALL product id 
    $(".productid").val(0); 
    /////////////////////////////////////////////////////////// 

    //note the url will break if site changed 
    $.ajax({url:"/mysite/products/list_products_from_range/"+$id, success:function(result){  

     /*if(result.length==0){ 
      //no results from ajax request 
      alert('No products found for this range.); 
      return false; 
     }*/ 

     //parse the returned JSON object,for each parse of result we check the table 
     $.each(jQuery.parseJSON(result), function() {  

      //console.log("product id="+this['Product']['id']); 

      pid=this['Product']['id']; 
      var price=this['Product']['price']; 
      var height=this['Product']['height']; 
      var width=this['Product']['width']; 

      /*console.log("price="+price); 
      console.log("h="+height); 
      console.log("w="+width);*/ 


      ///////////////////////////////////////////////////////////// 
      //now we have to go through the table and insert the values  

      i=-1; 

      var rows = $("#productentry tr:gt(0)"); // skip the header row    
      rows.each(function(index) {  

       i++; 
       var h = $("td:eq(3) .h", this).val(); 
       var w = $("td:eq(4) .w", this).val(); 

       //console.log(h +'x'+w);     
       //console.log("if "+w+" = "+width+" and "+h+" = "+height);    
       //console.log('index='+index); 


       if(w==width && h==height){ 
        //increment count of loaded values 
        loadedValues++; 

        //set the price 
        $("#listprice_"+i).val(price); 

        //set the pid 
        //alert(pid); 
        $("#productid_"+i).val(pid); 

        //change price textbox to visually show its chnaged 
        $("#listprice_"+i).css("background-color","#F60"); 

        //update totals (notworking) 
        calculateTotal(i);     

        return false; 
       } 

      }); 

      ///////////////////////////////////////////////////////////// 


     }); 
     alert('loaded '+loadedValues+' prices'); 


    }});  



    });//end click event 
}); 


///////////////////////////////////////////////////// 
//any input field that changes updates the calculation, not working fully i.e load product prices 
///////////////////////////////////////////////////// 
$(document).ready(function(){ 
    $(":input").change(calculateTotal); 
}); 


//////////////////////////////////// 
//calculate total 
/////////////////////////////////// 
var calculateTotal = function(index){ 

    var $discountpercent = null; 
    var $total=null; 
    var $quantity=null; 
    var $id=null; 
    var $marginpercent=null; 
    var $margintotal=null; 
    var myArray=null; 

    console.log('index='+index + ' indexlen= '+index.length); 

    if(index === undefined){  
     console.log('getting id'); 
     //get id of textbox 
     $id=$(this).attr('id'); 
     //get the row id 
     $id=$id.toString(); 
     myArray = $id.split('_'); 
     $id=myArray[1]; 
    } 
    else 
    { 
     console.log('setting id=index'); 
     $id=index;  
    } 

    var $listprice= $("#listprice_"+$id).val(); 

    //turn entered number into % 
    $discountpercent= $("#discountpercent_"+$id).val()/100; 
    $discountlistprice=$listprice-($listprice*$discountpercent);  

    //turn margin to % (note margin is global) 
    $marginpercent= $("#marginpercent_"+$id).val()/100; 

    //apply margin % to DLP 
    if($discountlistprice>0) 
    { 
     $margintotal=$discountlistprice+($discountlistprice*$marginpercent); 
    } 
    else 
    { 
     $margintotal=0; 
    } 

    //set rrp 
    $rrp=$margintotal; 
    $("#rrp_"+$id).val($rrp); 

    //quantity 
    $quantity=$("#quantity_"+$id).val(); 

    //calculate total 
    $total=$quantity*$rrp; 

    //set the value 
    $("#discountlistprice_"+$id).val($discountlistprice); 

    //set the total by changing the total div 
    $("#total_"+$id).html($total); 

} 
+0

你并没有显示在您尝试并调用该函数。任何jQuery事件处理程序的第一个参数是'event'对象。 – Pointy 2012-03-27 13:05:53

+0

我现在发布了 – 2012-03-27 13:15:20

回答

3

更改您的处理程序,以匿名函数:

$(document).ready(function(){ 
    $(":input").change(function() { 
     var index = getIndex(); // however you are getting this value 
     calculateTotal(index); 
    }); 
});