2012-04-09 143 views
-2

由於某些原因,我無法獲取變量'total'來定義所有...Javascript變量沒有定義

我在74上定義它,但它不想因爲某些原因而粘住..我究竟做錯了什麼?提前致謝!

$(document).ready(function() { 

    function getParameterByName(name) 
    { 
     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
     var regexS = "[\\?&]" + name + "=([^&#]*)"; 
     var regex = new RegExp(regexS); 
     var results = regex.exec(window.location.search); 
     if(results == null) 
      return ""; 
     else 
      return decodeURIComponent(results[1].replace(/\+/g, " ")); 
    } 

    $(".tab-content").hide(); //Hide all content 
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab 
    $(".tab-content:first").show(); //Show first tab content 


    $('.question-form-submit').click(function(e) { 
     e.preventDefault(); 
     var activeTab = '#'+$(this).attr('name'); 
     var activeClass = activeTab.substr(5); 
     $("ul.tabs li").removeClass("active"); 
     $('ul li:nth-child('+activeClass+')').addClass("active"); //Add "active" class to selected tab 
     $(".tab-content").hide(); //Hide all tab content 
     $(activeTab).fadeIn(); //Fade in the active ID content 


     $('.meter-value').removeClass('meter-width'); 

     switch (activeClass) { 
      case '2' : 
       $('.meter-value').attr('style', 'background-color: #9496c9; width: 46.5%;'); 
       break; 

      case '3' : 
       $('.meter-value').attr('style', 'background-color: #9496c9; width: 67%;'); 
       break; 

      case '4' : 
       $('.meter-value').attr('style', 'background-color: #9496c9; width: 100%;'); 
       break; 

     } 

     return false; 
    }); 

    $('.quantity, .init_cost').change(function() { 

     var row_id = $(this).attr('id'); 
     var row_number = row_id.substr(9); 
     var item_cost = $('#cost_'+row_number).attr('value'); 
     var item_quantity = $('#quantity_'+row_number).attr('value'); 
     var final_cost = item_cost * item_quantity; 

     $('#final_cost_'+row_number).val(final_cost).formatCurrency();; 

    }); 

    $('.row input').each(function(index) { 

     var row_id = $(this).attr('id'); 
     var row_number = row_id.substr(9); 
     var item_cost = $('#cost_'+row_number).attr('value'); 
     var item_quantity = $('#quantity_'+row_number).attr('value'); 
     var final_cost = item_cost * item_quantity; 

     $('#final_cost_'+row_number).val(final_cost).formatCurrency();; 

    }); 

     var total = 0; 
     $('.final_cost').each(function(index) { 

      var final_cost = $(this).attr('value').substr(1); 
      var total = total + final_cost; 
      console.log(total); 

     }) 

}); 
+1

你有什麼錯誤? – 2012-04-09 19:13:11

+0

這篇文章中的代碼太多了,沒有行號,所以很難看到你在說什麼。 – 2012-04-09 19:13:51

+1

如果我只能在所有代碼中找到'total' – Ibu 2012-04-09 19:14:21

回答

3

total在每個功能是陰影外一個。

同一事物的一個簡單的例子是here

(function() 
{ 
    var total = 1; 
    console.log("total 1: " + total); 
    (function() 
     { 
      console.log("total 2: " + total); 
      var total = total + 3; 
      console.log("total 3: " + total); 
     })() 
})(); 

除了陰影,你必須要考慮hoisting。由於內變量被提升到頂部,內部功能大致相當於:

function() 
{ 
    var total = undefined; 
    console.log("total 2: " + total); 
    total = total + 3; 
    console.log("total 3: " + total); 
} 

在這種情況下,我覺得你根本就不想內var關鍵字。在其他情況下,您將使用不同的變量名稱。

4

var total = total + final_cost;上的內部聲明隱藏行var total = 0;的外部聲明。

1

你通過

$('.final_cost').each(function(index) { 

    var final_cost = $(this).attr('value').substr(1); 
    var total = total + final_cost; 
    console.log(total); 

}) 

每次重新定義你總循環爲什麼不試試呢?

$('.final_cost').each(function(index) { 

    var final_cost = $(this).attr('value').substr(1); 
    total = total + final_cost; 
    console.log(total); 

}) 
0

那是因爲你聲明變量兩次(每次你寫var total它宣佈重新。所以你有「final_cost」功能和一個裏面,這是設置爲從外部+ final_cost總數的外面。所以,事實上你隨時登錄final_cost的價值。

只要寫total = total + final_cost;,它應該工作。

1

嘗試總之前刪除第二變種

total = total + final_cost; 
0

語法錯誤,殺死你的腳本:

$('#final_cost_'+row_number).val(final_cost).formatCurrency();; 
                    ^--- extra ; 

下一次,檢查瀏覽器的控制檯(按住Shift鍵CTRL-J的FF/Chrome瀏覽器)的錯誤。像這樣的事情會立即報告。

+0

我並不認爲額外的分號導致JS中的問題 – Matt 2012-04-09 19:18:36

+0

如果它是一個語法錯誤,它會立即被報告。 – 2012-04-09 19:21:58

0

我更換行:

var total = total + final_cost; 

有:

total += final_cost 
0

這個答案不知道,但嘗試一樣,默認值爲0:首先規定總變量,然後使用總計+最終成本操作。

var total = 0; 
total += final_cost; 

這是爲什麼?當你聲明的總沒有默認值,該值將是「不確定」這樣的JavaScript將代表以下爲:

var total = "undefined" + final_cost; 

我想這是錯誤。