2010-11-23 182 views
0

如何從函數A獲取值到函數B,或者? 我的代碼是一樣如何從一個函數獲取值到另一個函數

var $quantities = $('input.PackingCharge'); 
$(".prdItems tr").toggle(function() { 
    var totalQuantity = 0; 
    $(this).attr('clickedItem', '1'); 

    //Calculate the TotalValu 
    $quantities.each(function() { 
     var GAtrr = (this.parentNode.parentNode); 
     if (GAtrr.getAttribute('clickeditem') == '1') { 
      var quantity = parseFloat(this.value); 
      totalQuantity += quantity; 
     } 
    }); 
    // Total Value i hav append to the div and show the Total Value 
    $('#materialsCast').html(parseFloat(totalQuantity).toFixed(2)); 
}, function() { 
    // when i click this function the value not showing 
    console.log(totalQuantity.toFixed(2) + 'After'); 
    // Wht are the item cliked that i clike again the value need to subtration 
    $quantities.each(function() { 
     var GAtrr = (this.parentNode.parentNode); 
     if (GAtrr.getAttribute('clickeditem') == '1') { 
      var quantity = parseFloat(this.value); 
      totalQuantity -= quantity; 
     } 
    }); 
}); 

請幫助我減去當我們取消選擇所選擇的項目。

+0

如果這是更好的格式化它會更容易得到答案 – NimChimpsky 2010-11-23 14:06:19

回答

1

你可以通過總體每次剛開總的點擊者中,像這樣簡化它:,我們只是使用.toggleClass()添加

$(".prdItems tr").click(function(){ 
    $(this).toggleClass('clicked'); 

    var totalQuantity = 0;      
    $('.prdItems tr.clicked input.PackingCharge').each(function() { 
     totalQuantity += parseFloat(this.value); 
    }); 
    $('#materialsCast').html(parseFloat(totalQuantity).toFixed(2));  
}); 

取而代之的是自定義屬性/刪除clicked類每次點擊,並使用相同的類來計算總數時選擇「在」行。這裏的好處是,你現在可以定義一個CSS類去用它添加一些造型自由,就像這樣:

.clicked { background-color: green; } 
+0

HI尼克 謝謝對於給我答案,但是當你取消選中同一行時,該值需要從totalQuantity減去(減去)。如何做到這一點? – VijayKR 2010-11-23 14:46:27

相關問題