javascript - 为什么这个 JavaScript 数组 for 循环不像字面版本那样工作?

标签 javascript refactoring loops

我是 javascript 新手,正在尝试重构一些代码,显然我在 javascript 中缺少一些我想学习的东西。一旦所有 5 个列表框都选择了某些内容,此代码就会生成一个值:

function GetTotal() {
        //_listSeverity = document.getElementById("_listSeverity");
        function ParseListBoxvalue(listBox) {
        return parseInt(GetListBoxValue(listBox),10);
        }
        _listSeverity = document.getElementById("<%= _listSeverity.ID %>");
        _listAssociate = document.getElementById("<%= _listAssociateImpact.ID %>");
        _listCustomerImpact = document.getElementById("<%= _listCustomerImpact.ID %>");
        _listRegulatoryImpact = document.getElementById("<%= _listRegulatoryImpact.ID %>");
        _listShareholderImpact = document.getElementById("<%= _listShareholderImpact.ID %>");
        _calculatedTotal = (ParseListBoxvalue(_listAssociate) +
            ParseListBoxvalue(_listSeverity) + ParseListBoxvalue(_listCustomerImpact) 
           +ParseListBoxvalue(_listRegulatoryImpact) + ParseListBoxvalue(_listShareholderImpact)
          )/ 5;
        if (isNaN(_calculatedTotal))
            document.getElementById("_total").innerHTML = "Not enough information";
        else
            document.getElementById("_total").innerHTML = _calculatedTotal;
    }

然后我尝试重构为 for 循环以消除一些代码重复。 我尝试了很多 if(typeof _calculatedValue !='undefined') 方法,我在谷歌上找到了它,看看是否可以解决这个问题。据我了解,我在这里没有遇到范围问题,因为唯一的实际范围受 function(){} 声明的限制。 这永远不会产生值(value)。我意识到 /5 尚未包含在其中,但这对我来说似乎并不是始终产生 NaN 的原因。

 function GetTotal() {
        //_listSeverity = document.getElementById("_listSeverity");
        function ParseListBoxvalue(listBox) {
        return parseInt(GetListBoxValue(listBox),10);
    }
        var _ListIds=new Array("<%= _listSeverity.ID %>","<%= _listAssociateImpact.ID %>",
            "<%= _listCustomerImpact.ID %>", "<%= _listRegulatoryImpact.ID %>",
            "<%= _listShareholderImpact.ID %>");
//            _calculatedTotal = (ParseListBoxvalue(_listAssociate) +
//                ParseListBoxvalue(_listSeverity) + ParseListBoxvalue(_listCustomerImpact) 
//               +ParseListBoxvalue(_listRegulatoryImpact) + ParseListBoxvalue(_listShareholderImpact)
        //              )/ 5;
        for (i = 0; i < _ListIds.length; i++) {
            if (i==0)
                _calculatedTotal = ParseListBoxvalue(_ListIds[i]);
            else
                _calculatedTotal += ParseListBoxvalue(_ListIds[i]);
        }

        if (isNaN(_calculatedTotal))
            document.getElementById("_total").innerHTML = "Not enough information";
        else
            document.getElementById("_total").innerHTML = _calculatedTotal;
    }

其他函数应该不相关,但它是:

function GetListBoxValue(listBox) {
        index = listBox.selectedIndex
        try {
            opt = listBox.options[index]
            return opt.value;
        } catch (e) { return null; }

    }

这个 for 循环有什么问题?或者是除了 for 循环之外的其他原因导致重构不产生值?

最佳答案

您没有调用document.getElementById():

_calculatedTotal = ParseListBoxvalue(_ListIds[i]);

应该是

_calculatedTotal = ParseListBoxvalue(document.getElementById(_ListIds[i]));

其他分支也类似。

关于javascript - 为什么这个 JavaScript 数组 for 循环不像字面版本那样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1600250/

相关文章:

php - 需要一些关于计数和计算行总和的指导。

java - for循环将数组元素实例化为一个对象,但我还是必须手动实例化?

javascript - 在浏览器中上传文件并再次将其保存到我的硬盘上

javascript - 将函数参数传递给另一个函数

javascript - 使用 CSS 或 jQuery 的梯形响应式 Div

javascript - 重构复杂的嵌套 Node.js 函数

javascript - 嵌套 AJAX 回调函数的可读性

javascript - 如何确定网页是否启用了 jquery?

architecture - 您能推荐一些关于模块化设计的好书吗?

Javascript - P 标签上 maxLength 的 For 循环