2012-04-09 94 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; 

我想这是错误。