2017-06-17 10 views
0

私は同じ機能を持つさまざまな引数をさまざまなボタンに割り当てています。私はこの機能を呼び出すために3番目のボタンを作ったが、機能しません。それはJavaScriptの制限ですか?またはこれを行う方法はありますか?別個のボタンを使用して2回以上関数を呼び出す

HTML:

<head> 
    <link rel='stylesheet' type='text/css' href='css/styles.css'> 
</head> 

<body> 

    <p id="para">dud</p> 

    <button type="button" onclick="monsterFound();">Test monster</button> 
    <button type="button" onclick="d20();">Test a Die 20</button> 
    <button type="button" onclick="showChance();">Test Chance</button> 
    <button type="button" onclick="d20(); damageAccuracy(damage,dice1,calculateChance());">Test Accuracy</button> 
    <button type="button" onclick="equip(loot[1]);">equip weapon1</button> 
    <button type="button" onclick="equip(loot[5]);">equip weapon2</button> 
    <button type="button" onclick="equip(loot[8]);">equip weapon3</button> 

    <script src='https://code.jquery.com/jquery-3.1.0.min.js'></script> 
    <script src='main.js'></script> 
</body> 

JS:

var loot = [ 'rusty iron dagger', 'rusty iron claymore', 'rusty iron axe', 
'rusty iron hammer', 'rusty iron spear', 'rusty iron warhammer', 'rusty iron 
waraxe', 'rusty iron short sword', 'rusty iron long sword', 'rusty iron 
helmet', 'rusty iron breast-plate', 'rusty iron greaves', 'rusty iron left 
pauldron', 'rusty iron right pauldron']; 
// x = 14; 
var onHandOut = 0; 
var offHandOut = 0; 
var onHand = "empty"; // right hand 
var offHand = "empty"; // left hand 
var damage = 0; 
// when you equip a weapon, base damage is set ------------------- 
alert(onHand); 
alert(damage); 

function equip(item) { 
onHand = item; 

if (onHand === "empty") { 
    alert("Empty"); 
    onHandOut = 0.65; 
    damage = onHandOut; 

} else if (onHand === "rusty iron dagger") { 
    onHandOut = 1.25; 
    damage = onHandOut; 
    alert("Dagger equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron claymore") { 
    onHandOut = 2.25; 
    damage = onHandOut; 
    alert("claymore equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron axe") { 
    onHandOut = 1.25; 
    damage = onHandOut; 
    alert("axe equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron hammer") { 
    onHandOut = 1.5; 
    damage = onHandOut; 
    alert("hammer equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron spear") { 
    onHandOut = 1.75; 
    damage = onHandOut; 
    alert("spear equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron warhammer") { 
    onHandOut = 2.5; 
    damage = onHandOut; 
    alert("warhammer equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron waraxe") { 
    onHandOut = 2.25; 
    damage = onHandOut; 
    alert("waraxe equiped, Damage = " + onHandOut); 

} else if (onhand === "rusty iron short sword") { 
    onHandOut = 2.25; 
    damage = onHandOut; 
    alert("short sword equiped, Damage = " + onHandOut); 

} else if (onHand === "rusty iron long sword") { 
    onHandOut = 2.5; 
    damage = onHandOut; 
    alert("long sword equiped, Damage = " + onHandOut); 

} else if (onhand === "empty") { 
    onHandOut = 0.625; 
    damage = onHandOut; 
    alert("no weapon equiped, Damage = " + onHandOut); 

} else { 
    // Do nothing! 
}  

}

答えて

1

実は、それは(あなたのコードにちょうどタイプミスですonhand onHandの代わりに)。あなたはこのラインを持っている

} else if (onhand === "rusty iron short sword") { 

それは次のようになります。

} else if (onHand === "rusty iron short sword") { 
+0

私はタイプミスを修正したが、3番目のボタンはまだ動作しません:( –

+1

私はタイプミスを修正したが、第三ボタンはまだ動作しません:(更新*:働いて、ありがとうございます:) –

関連する問題