试图访问另一个函数的值

时间:2016-04-02 15:30:00

标签: javascript jquery html

我试图从另一个函数中获取一个值,但它继续在我的span标签上显示NaN,方法是将我的函数getSummary()放在html的body标签中onload上

function calculate(){
    var coke = document.getElementById('drinks-coke').value;
    var royal = document.getElementById('drinks-royal').value;
    var sprite = document.getElementById('drinks-sprite').value;

    var drink1 = coke * 35;  
    var drink2 = royal * 35;  
    var drink3 = sprite * 35;  

    // "totaldrinks":
     var totald = document.getElementById("total-drinks").innerHTML = drink1+drink2+drink3; 
}

function calculateAddons(){
    var rice1 = document.getElementById('addons-rice').value;
    var sauce1 = document.getElementById('addons-sauce').value;
    var protein1 = document.getElementById('addons-protein').value;
    var vegies1= document.getElementById('addons-vegies').value;

    var addon1 = rice1 * 20;  
    var addon2 = sauce1 * 30;  
    var addon3 = protein1 * 70;  
    var addon4 = vegies1 * 50;  

    // "totaladdons": 
    var totala = document.getElementById("total-addons").innerHTML = addon1+addon2+addon3+addon4; 
}

function getSummary() {
    var riceData = document.getElementById("ricingbowlfinal").getAttribute("value");
    var deliveryData = document.getElementById("deliverycharge").getAttribute("value")
    var totalbill = parseInt(riceData) + parseInt(deliveryData);
    var totaldr = calculateAddons("totala")
    var totalad = calculate("totald")

    document.getElementById("summarys").innerHTML = totalbill + totaldr + totalad;
}

3 个答案:

答案 0 :(得分:1)

您需要返回值并修复调用函数的方式。您在调用时将字符串传递给函数,但这是不必要的。

for

应该这样做。

答案 1 :(得分:0)

如果您只需要(例如)计算中的一个值,请将其更改为返回值:

function calculate(){
    var coke = document.getElementById('drinks-coke').value;
    var royal = document.getElementById('drinks-royal').value;
    var sprite = document.getElementById('drinks-sprite').value;

    var drink1 = coke * 35;  
    var drink2 = royal * 35;  
    var drink3 = sprite * 35;  

    // "totaldrinks":
     var totald = document.getElementById("total-drinks").innerHTML = drink1+drink2+drink3; 

   return totald;
}

调用代码然后可以使用:

获取该值
var total = calculate();

答案 2 :(得分:0)

Calculate()和calculateAddons()中缺少

return语句 这是您的解决方案,

function calculate(){
    var coke = document.getElementById('drinks-coke').value;
    var royal = document.getElementById('drinks-royal').value;
    var sprite = document.getElementById('drinks-sprite').value;

    var drink1 = coke * 35;  
    var drink2 = royal * 35;  
    var drink3 = sprite * 35;  

    // "totaldrinks":
     var totald = document.getElementById("total-drinks").innerHTML = drink1+drink2+drink3; 
     return totald;
}

function calculateAddons(){
    var rice1 = document.getElementById('addons-rice').value;
    var sauce1 = document.getElementById('addons-sauce').value;
    var protein1 = document.getElementById('addons-protein').value;
    var vegies1= document.getElementById('addons-vegies').value;

    var addon1 = rice1 * 20;  
    var addon2 = sauce1 * 30;  
    var addon3 = protein1 * 70;  
    var addon4 = vegies1 * 50;  

    // "totaladdons": 
    var totala = document.getElementById("total-addons").innerHTML = addon1+addon2+addon3+addon4; 
     return totala;
}

function getSummary() {
    var riceData = document.getElementById("ricingbowlfinal").getAttribute("value");
    var deliveryData = document.getElementById("deliverycharge").getAttribute("value")
    var totalbill = parseInt(riceData) + parseInt(deliveryData);
    var totaldr = calculateAddons("totala")
    var totalad = calculate("totald")

    document.getElementById("summarys").innerHTML = totalbill + totaldr + totalad;
}