的jQuery/JavaScript的 - 传递变量函数
我有一个功能,看起来像这样:的jQuery/JavaScript的 - 传递变量函数
window.attack = function(enemyHealth){
attackChance = Math.floor(Math.random()*11);
console.log("enemyHealth: "+enemyHealth); //undefined...
if (attackChance < 5) { //hit
hitDamage = Math.floor(Math.random()*playerDamage+1);
enemyHealth = enemyHealth - hitDamage;
$('#enemyhealth').val(enemyHealth);
if (enemyHealth < 1) {
alert('You killed the '+currentEnemy);
removeOverlayNow();
}
} else if (attackChance >= 5) { //miss
hitDamage = Math.floor(Math.random()*maxDamage+1);
alert('You miss and get attacked for '+hitDamage);
health = health - hitDamage;
$(healthDisplay).val(health);
checkHealth();
}
}
而且我的变量正在通过抛出:
onclick="flee('+currentEnemyHealth+')"
但是,当我console.log
或alert()
我得到的全部是Undefined
...
我错过了一个小把戏吗?我认为如果我将一个变量传入函数(在这种情况下为enemyHealth
),我应该能够检索它?
谢谢。
p.s.
只是想我要指出,即使我通过函数传递卡编码的数字(如5) - 它仍然会到达作为Undefined
你应该联系
onclick="flee(" + currentEnemyHealth + ")"
希望这有助于。
d'oh - 不要说一句话。 – 2010-08-02 13:54:30
常见错误。在艰苦的一天工作中责备它。 – 2010-08-02 14:37:42
你确定你不是说:
onclick="flee("+currentEnemyHealth+")"
修复引用,使currentEnemyHealth的值嵌入到字符串中?
currentEnemyHealth不在javascript调用的范围内。
哪里是调用window.attack的代码?从它看起来没有传递一个值 – DanSingerman 2010-08-02 13:18:07