简体   繁体   English

Javascript函数第一次运行但不会再运行

[英]Javascript function runs the first time but won't again

Context behind this, there is another function that listens for a change in an HTML dropdown. 在此背后的上下文中,还有另一个函数可以监听HTML下拉列表中的更改。 When there is a change, the JavaScript calls the costs function and puts in the new variables, which should recalculate the cost. 当有变化时,JavaScript调用成本函数并输入新变量,这应该重新计算成本。

However, after running this the first time, the code will not run again. 但是,第一次运行此代码后,代码将不再运行。 I know this because the alert "I AM RUNNING" only appears when the function is called the first time. 我知道这一点,因为只有在第一次调用该函数时才会出现“我正在运行”警报。

Any idea what I am overlooking? 知道我在俯瞰什么吗?

function costs(f, p) {
        window.alert("I AM RUNNING");
        costs = [10500, 33950, 33950, 22576, 22576, 22576, 39324, 39324, 39324, 39324, 39324, 0];
        models = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "\u00a0"];
        ind = models.indexOf(f);
        a = ((p/100)*costs[ind]).toFixed(2);
        b = a.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        c = "$"+b;
        return c;
    };

Thanks! 谢谢!

You're redefining costs within the function at line: 您正在重新定义函数内的costs

costs = [10500, 33950, 33950, 22576, 22576, 22576, 39324, 39324, 39324, 39324, 39324, 0];

Make it a different variable name. 使其成为不同的变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM