javascript - 在另一个函数中使用函数的值

时间:2021-10-06 20:04:42

so i have made a simple price quote script with the basic knowledge (very basic) i have taught myself and i would like to add some more stuff to it...

所以我已经制作了一个简单的价格报价脚本,其中包含我自学的基本知识(非常基础),我想为它添加一些东西......

here is the price quote code on jsfiddle

这是jsfiddle的价格报价代码

i have check boxes but they have no script attached to them and i would like to add a value to be added to to the total when the checkbox is check...i found a code for a simple checkbox

我有复选框,但他们没有附加脚本,我想添加一个值,当复选框被检查时添加到总...我找到一个简单的复选框的代码

function initialize() {
    Total = 0;
    totalprice.innerText = Total;
}

function checkoption(checkbox) {
    checknum = parseInt(checkbox.value);
    if (checkbox.checked == true) {
        Total += checknum;
    } else {
        Total -= checknum;
    }
    totalprice.innerText = Total;
}

How do i get the value created from these two functions (it being 0 if no check boxes are selected) and use it in the calculate function...do i parsefloat something? in this example code is the value Total? and lastly do the functions run in order from top to bottom...therefore these two example functions should go on top of function calculate?

我如何获得从这两个函数创建的值(如果没有选中复选框则为0)并在计算函数中使用它...我解析了什么?在这个例子中代码是值Total?并且最后按顺序从上到下运行函数...因此这两个示例函数应该在函数计算之上?

ive been trying different things and can seem to find what to do :S....

我一直在尝试不同的东西,似乎可以找到该做什么:S ....

1 个解决方案

#1


1  

The value you want is in the Total variable, which is global (a discouraged practice btw). The function declaration order doesn't matter as long as it's not called before it's definition.

你想要的值是Total变量,它是全局的(bww气馁)。函数声明顺序无关紧要,只要它在定义之前没有被调用。

#1


1  

The value you want is in the Total variable, which is global (a discouraged practice btw). The function declaration order doesn't matter as long as it's not called before it's definition.

你想要的值是Total变量,它是全局的(bww气馁)。函数声明顺序无关紧要,只要它在定义之前没有被调用。