简体   繁体   English

如果javascript中的else语句不起作用。 仅在else语句中

[英]if else statement in javascript is not working. only in else statement

Sorry, i'm still new here. 对不起,我还是新来的。 i just want to know where's my error here. 我只想知道我的错误在哪里。 my else statement is not working. 我的else陈述不起作用。 but the if statement is working. 但是if语句有效。 lets assume that all my variables are correct though my conditional statement isnt. 让我们假设我的条件语句不是,但我的所有变量都是正确的。 i really need help, im stuck here. 我真的需要帮助,我被困在这里。

expecting result: 预期结果:

i need to add 13 options for my select. 我需要为我的选择添加13个选项。 but when my if statement is done, and the else statment will take over the variable hr1 , it stop and skip the process. 但是当我的if语句完成后,else语句将接管变量hr1 ,它将停止并跳过该过程。 wherein the option is not added. 其中未添加该选项。

for example: 例如:

hr1 = 9
convertedVar = 20

->it will only add the option when the condition is satisfied, if(hr1 <= convertedVar) and then else will skip. ->仅在满足条件时添加选项, if(hr1 <= convertedVar) ,则其他选项将跳过。

thank you for you help. 谢谢你的帮助。

if(a==mod)
{
    for(i = 0; i <= 13 ; i++) 
    { 
        if(hr1 <= convertedVar)
        {
        document.getElementById('timeId').options[i] = new Option('---','');
        hr1++;
        hr2++;
        }
        else
        {
        document.getElementById('timeId').options[i] = new Option(h1++ + ':00', hr2++ + ':00');
        }   
    }
}

Don't know much about the logic behind this but this seems a typo to me: 对这背后的逻辑了解不多,但这对我来说是错别字:

.... s[i] = new Option(h1++ + ':00', hr2 ....
//                    h1 or hr1 ??

You are incrementing the variable h1 instead of the variable hr1 If h1 isn't known at that time, your loop will stop. 您将递增变量h1而不是变量hr1如果当时不知道h1则循环将停止。 As this is the described behavior, 99% sure that this is the error: just a typo. 由于这是描述的行为,因此99%的人确保这是错误:只是错字。

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

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