您的问题是您在页面加载时设置字符描述字符串,在用户选择派系之前。
// curFaction is undefined here, as user cannot select a faction
// *before* the page is loaded!!
var aLevel[1] = "Description1 of guy from " + curFaction + ".";
最快的解决方法是在需要时生成描述。一种方法是将函数存储在 中aLevel
,而不是字符串,如下所示:
var aLevel[1] = function () { "Description1 of guy from " + curFaction + "."; };
var aLevel[2] = function () { "Description2 of guy from " + curFaction + "."; };
var aLevel[3] = function () { "Description3 of guy from " + curFaction + "."; };
然后,当您需要您的描述时,调用该函数:
// Original code:
"Stuff about claim on throne. " + aLevel[temp6] + ".";
// New code - note brackets after "aLevel[temp6]"
"Stuff about claim on throne. " + aLevel[temp6]() + ".";
我创建了一个 Fiddle 来展示这一点。
顺便说一句,您的代码通常可以重构很多 - 通常,如果您发现自己一遍又一遍地编写相同的算法,那么您需要记住DRY 原则:)