我有以下功能,它会一直返回True。任何想法为什么以及如何避免它?谢谢你们。为什么下面的javascript函数总是返回true?
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
它返回'true',因为在某些时候'if(value =='new')'将条件评估为'false',因此驱动代码流向'else'分支。 – 2012-01-09 15:14:14
任何时候你必须评论块的结尾''}“'通常意味着块太长。 – 2012-01-09 15:14:23
偏离主题,但是由于您似乎正在使用jQuery,因此您可以查看它的['val'](http://api.jquery.com/val/)和['each'](http:// api .jquery.com/val /)函数,这可能有助于简化代码。 – 2012-01-09 15:17:50