Javascript函数就不会运行了

时间:2016-10-23 21:00:05

标签: javascript html function

这个脚本不起作用,我找不到原因。它应该根据结果设置一个“p”元素,但它不会起作用。我已经包含了下面的脚本。我只是刚开始学习JavaScript,所以这可能是一个愚蠢的错误。

JSON.parse
var spearman = 12;
var swordsman = 10;
var berserker = 68;
var archer = 16;
var crossbowman = 13;
var nordicArcher = 56;
var armouredHorseman = 22;
var lancerHorseman = 20;
var axeRider = 82;
var handCart = 500;
var oxCart = 2500;
var spearmanCap;
var swordsmanCap;
var berserkerCap;
var archerCap;
var crossbowmanCap;
var nordicArcherCap;
var armouredHorsemanCap;
var lancerHorsemanCap;
var axeRiderCap;
var handCartCap;
var oxCartCap;
var numberOfCastles;
var numberOfForts;
var silverRequired;
var troopsCanCarry;

function enoughSilver() {
    spearmanCap = spearman * document.getElementById("spearmanNum").value;
    swordsmanCap = swordsman * document.getElementById("swordsmanNum").value;
    berserkerCap = berserker * document.getElementById("berserkerNum").value;
    archerCap = archer * document.getElementById("archerNum").value;
    crossbowmanCap = crossbowman * document.getElementById("crossbowmanNum").value;
    nordicArcherCap = nordicArcher * document.getElementById("nordicArcherNum").value;
    armouredHorsemanCap = armouredHorseman * document.getElementById("armouredHorsemanNum").value;
    lancerHorsemanCap = lancerHorseman * document.getElementById("lancerHorsemanNum").value;
    axeRiderCap = axeRider * document.getElementById("axeRiderNum").value;
    handCartCap = handCart * document.getElementById("handCartNum").value;
    oxCartCap = oxCart * document.getElementById("oxCartNum").value;
    
    troopsCanCarry = spearmanCap + swordsmanCap + berserkerCap + archerCap + crossbowmanCap + nordicArcherCap + armouredHorsemanCap + lancerHorsemanCap + axeRiderCap + handCartCap + oxCartCap;
    
    numberOfCastles = document.getElementById("attackerCastlesNum");
    numberOfForts = document.getElementById("attackerFortNum");
       
    if (document.getElementById("landRefYes").checked) {
        if (document.getElementById("yourcastle").checked) {     
            numberOfForts = numberOfForts * 5;
            numberOfCastles = numberOfCastles + numberOfForts;
            silverRequired = 1000 * numberOfCastles;
            silverRequired = silverRequired / 2;
            if (troopsCanCarry == silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry exacly the right amount of silver. This is almost certainly a real attack";
            }
            else if (troopsCanCarry > silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry more than enough silver to capture your castle. Keep your troops inside your castle and consider calling support!";
            }
            else if (troopsCanCarry < silverRequired) {
                document.getElementById("answer").innerHTML = "The troops cant carry enough silver this isnt a real attack";
            }
        }
        else if (document.getElementById("yourfort").checked) {
            numberOfForts = numberOfForts * 5;
            numberOfCastles = numberOfCastles + numberOfForts;
            numberOfCastles = numberOfCastles * 5;
            silverRequired = 1000 * numberOfCastles;
            if (troopsCanCarry == silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry exacly the right amount of silver. This is almost certainly a real attack";
            }
            else if (troopsCanCarry > silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry more than enough silver to capture your castle. Keep your troops inside your castle and consider calling support!";
            }
            else if (troopsCanCarry < silverRequired) {
                document.getElementById("answer").innerHTML = "The troops cant carry enough silver this isnt a real attack";
            }
        }
        else {
            console.log("Error")
        }
        
        
    }
    else if (document.getElementById("landRefNo").checked) {
        if (document.getElementById("yourcastle").checked) {     
            numberOfForts = numberOfForts * 5;
            numberOfCastles = numberOfCastles + numberOfForts;
            silverRequired = 1000 * numberOfCastles;
            if (troopsCanCarry == silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry exacly the right amount of silver. This is almost certainly a real attack";
            }
            else if (troopsCanCarry > silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry more than enough silver to capture your castle. Keep your troops inside your castle and consider calling support!";
            }
            else if (troopsCanCarry < silverRequired) {
                document.getElementById("answer").innerHTML = "The troops cant carry enough silver this isnt a real attack";
            }
        }
        else if (document.getElementById("yourfort").checked) {
            numberOfForts = numberOfForts * 5;
            numberOfCastles = numberOfCastles + numberOfForts;
            numberOfCastles = numberOfCastles * 5;
            silverRequired = 1000 * numberOfCastles;
            if (troopsCanCarry == silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry exacly the right amount of silver. This is almost certainly a real attack";
            }
            else if (troopsCanCarry > silverRequired) {
                document.getElementById("answer").innerHTML = "The troops can carry more than enough silver to capture your castle. Keep your troops inside your castle and consider calling support!";
            }
            else if (troopsCanCarry < silverRequired) {
                document.getElementById("answer").innerHTML = "The troops cant carry enough silver this isnt a real attack";
            }
        }
    else {
        console.log("Error");
    }
        
        
    }
}

提前致谢

2 个答案:

答案 0 :(得分:0)

我认为你忘记了那里的价值观:

{{1}}

答案 1 :(得分:0)

您的代码中存在一些小的语法错误(尽管这些不是您的问题的原因):

(1)您拼错了HTML标签&#34; table&#34; as&#34; tabel&#34 ;; &安培;

(2)你在其中一行的末尾省略了一个分号:

else {
    console.log("Error")
}

至于你的问题 -

代码:

numberOfCastles = document.getElementById("attackerCastlesNum");
numberOfForts = document.getElementById("attackerFortNum");

不会返回输入元素的值 - 而是返回代表元素的DOM对象的实例。

您应该使用 -

numberOfCastles = document.getElementById("attackerCastlesNum").value;
numberOfForts = document.getElementById("attackerFortNum").value;

此外,您还有以下结构:

if (troopsCanCarry == silverRequired) { ... }
else if (troopsCanCarry > silverRequired) { ... }
else if (troopsCanCarry < silverRequired) { ... }

你应该考虑用else交换第三个else-if条件;前两个测试确保满足第三个条件 - 例如

if (troopsCanCarry == silverRequired) { ... }
else if (troopsCanCarry > silverRequired) { ... }
else { ... }