简体   繁体   English

为什么以下javascript函数总是返回true?

[英]Why does the following javascript function always return true?

I have the following function, it will always return True. 我有以下功能,它将始终返回True。 Any ideas why and how to avoid it? 有什么想法为什么以及如何避免它? Thanks folks. 谢谢大家。

function validateStatuses(xyx){
var umm = ugh[xyx];
var selects = $('#cont_'+ugh.xyz+' .status_select');
var codes = $('#cont_'+ugh.xyz+' .status_code');
for (var i = 0; i < selects.length; i++) {
    var value = selects[i].options[selects[i].selectedIndex].value;
    if (value == 'new'){
        for (var j = 0; j < codes.length; j++) {
            var blagh = codes[j].options[codes[j].selectedIndex].value;
            if(blagh == 13){
                $('#info_dialog').html('');
                $('#info_dialog').append("<p>You are trying to process a bill ("+bill.name+") with a STATUS of NEW and a STATUS CODE of NONE. Please correct this issue before you proceed!</p><hr />");
                $('#info_dialog').dialog({
                    buttons:{
                        Cancel: function(){
                            $(this).dialog('close');
                        }
                    }
                    });
                billCounterAdd();
                return false;
            }//end if           
        }//end for
    }else{
        return true;  //this is the problem;
    }//end if
}//end for
}//end Function

I dare say you have at least one select whose value isn't 'new' . 我敢说您至少有一个选择的值不是'new' Because you've done a return true; 因为您已经完成了return true;return true; in the else clause, the first select with a value that isn't 'new' will cause the function to return true. else子句中,第一个选择值不是'new'将使函数返回true。

It looks like it does have a false return route (if there's a 'new' select at the beginning and there's a code select with the value 13 ), but perhaps that test case didn't come up in your testing. 看起来它确实具有错误的返回路线(如果在开始处有一个'new'选择,并且有一个代码选择带有值13 ),但是也许该测试用例没有出现在您的测试中。

In terms of figuring out what's wrong with things like this, there's nothing quite like walking through the code and watching it run line-by-line in a decent debugger. 就找出类似问题的出处而言,没有什么比遍历代码并在像样的调试器中逐行观察代码更像了。 All major browsers have them built in now (finally), so you can see exactly what's happening and inspect variables, etc. 所有主流浏览器现在(最终)都已内置它们,因此您可以确切地看到正在发生的事情并检查变量等。

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

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