出于某种原因,我根本无法定义变量'total'...
我把它定义为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);
})
});
答案 0 :(得分:4)
第var total = total + final_cost;
行上的内部声明隐藏了var total = 0;
行的外部声明。
答案 1 :(得分: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
关键字。在其他情况下,您将使用不同的变量名称。
答案 2 :(得分: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);
})
答案 3 :(得分:1)
尝试在总数之前删除第二个变量。
total = total + final_cost;
答案 4 :(得分:0)
那是因为你将变量声明了两次(每当你写var total
时它都被重新声明。所以你有一个在“final_cost”函数之外,一个在inside里面,从外部+ final_cost设置为total。所以实际上,您始终记录final_cost的值。
只需写下total = total + final_cost;
即可。
答案 5 :(得分:0)
语法错误,杀死你的脚本:
$('#final_cost_'+row_number).val(final_cost).formatCurrency();;
^--- extra ;
下次,检查浏览器的控制台(FF / Chrome中的shift-ctrl-J)是否有错误。这样的事情会立即报告。
答案 6 :(得分:0)
我会替换这一行:
var total = total + final_cost;
使用:
total += final_cost
答案 7 :(得分:0)
不确定这个答案,但是,尝试使用默认值(如0)首先定义变量total,然后使用total + final_cost操作。
var total = 0;
total += final_cost;
为什么?当你声明没有默认值的总数时,该值将是“未定义”,因此javascript将代表以下内容:
var total = "undefined" + final_cost;
我猜这是错误。