Javascript变量未定义错误

Javascript variable not defined error

本文关键字:错误 未定义 变量 Javascript      更新时间:2023-09-26

我有以下代码:

// Get combination prices
var combID = $('#idCombination').val();
var combinationsFromController;
var combination = combinationsFromController[combID];
if (typeof combination === 'undefined')
    return;
// Set product (not the combination) base price
var basePriceWithoutTax = +productPriceTaxExcluded;
var basePriceWithTax = +productPriceTaxIncluded;
var priceWithGroupReductionWithoutTax = 0;

当我执行它时,Chrome总是抱怨:

combinationsFromController is not defined.

有人能帮我为什么会发生这种事吗?

我试过了:

var combinationsFromController = new Array();
var combination = combinationsFromController[combID];

运气不佳。

您从未从控制器中定义组合。

var combinationsFromController;
// combinationsFromController is undefined because you did not give it a value

在使用变量之前,必须给它们一个值。

你可能想要

var combinationsFromController = getCombitationsFromController()

getCombitationsFromController()替换为代码以从控制器获取组合。