简体   繁体   English

我希望我的 javascript 在第一个 if 语句评估为 TRUE 时移动到下一个 if 语句

[英]I want my javascript to move to the next if statement if the first one evaluates as TRUE

I want to check for negative values and if there is change them to its default value.我想检查负值以及是否将它们更改为默认值。

for (i = 0; i <= 1; i++) {
    //I want this to run this statement once and if its true perform all the functions and move to the next one
    if (h1 < 0 || h2 < 0) {
        alert("negative number is not acceptable here");
        document.getElementById("up1").value = document.getElementById("up1").defaultValue;
        document.getElementById("q1").value = document.getElementById("q1").defaultValue;
        document.getElementById("tot1").value = document.getElementById("tot1").defaultValue;
        Total1 = 0.00;
        continue;

    }
    else if (h3 < 0 || h4 < 0) {
        alert("No Negative here as well")
        document.getElementById("up2").value = document.getElementById("up2").defaultValue;
        document.getElementById("q2").value = document.getElementById("q2").defaultValue;
        document.getElementById("tot2").value = document.getElementById("tot2").defaultValue;
        continue;

    }
    //I have a let Total1 & Total2 variables at the beggining of the code set to zero
    //
    Total2 = h3 * h4;
}    

Hi guys is there anyway to continue to the next else if statement if the first IF statement evaluates to TRUE and runs all the functions in it.大家好,如果第一个 IF 语句的计算结果为 TRUE 并运行其中的所有函数,无论如何都可以继续下一个 else if 语句。 Currently, if the first IF statement evaluates to true it performs all the functions in it and ends the loop.目前,如果第一个 IF 语句的计算结果为真,它会执行其中的所有函数并结束循环。 I want it to check each variable for a negative without repeating the code all over again.我希望它检查每个变量是否为负数,而无需再次重复代码。

There are two cases for your problem.您的问题有两种情况。

  1. You want to run the second block only if the first if block is true.您只想在第一个 if 块为真时运行第二个块。
  2. You want to run the second block either the first block is true or false你想运行第二个块,第一个块是 true 还是 false

First case第一个案例

isFirstBlockTrue = false
if (h1 < 0 || h2 < 0) {
   isFirstBlockTrue = true
   /* rest of your code */
}

if(isFirstBlockTrue){

  if (h3 < 0 || h4 < 0) {
    /* rest of your code */
  }

}

 /* rest of your code */

Second Case第二个案例

if (h1 < 0 || h2 < 0) {
  /* first block code */
}
if (h3 < 0 || h4 < 0) {
  /* second block code */
}

Note: Don't use the continue otherwise it will skip the iteration注意:不要使用 continue 否则它会跳过迭代

If you want to run the second block only if the first if block is true:如果您只想在第一个 if 块为真时运行第二个块:

if (h1 < 0 || h2 < 0) {
   /* Your code */

   if(h3 < 0 || h4 < 0){
       /* rest of your code */
   }
}

You just need two linear if statements.您只需要两个线性 if 语句。

for (i = 0; i <= 1; i++) {
    //I want this to run this statement once and if its true perform all the functions and move to the next one

    if (h1 < 0 || h2 < 0) {
        alert("negative number is not acceptable here");
        document.getElementById("up1").value = document.getElementById("up1").defaultValue;
        document.getElementById("q1").value = document.getElementById("q1").defaultValue;
        document.getElementById("tot1").value = document.getElementById("tot1").defaultValue;
        Total1 = 0.00;
    }
    

   if (h3 < 0 || h4 < 0) {
       alert("No Negative here as well")
       document.getElementById("up2").value = document.getElementById("up2").defaultValue;
       document.getElementById("q2").value = document.getElementById("q2").defaultValue;
       document.getElementById("tot2").value = document.getElementById("tot2").defaultValue; 
     }

    Total2 = h3 * h4;
}  

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

相关问题 为什么以下javascript语句的评估结果为true? - Why the following javascript statement evaluates to true? Javascript:如果语句始终评估为真,则为假 - Javascript: False if statement always evaluates as true JavaScript if语句仅评估第一个条件 - JavaScript if statement evaluates only first condition 条件语句为false &amp;&amp; true时,JavaScript代码评估为true - Javascript code evaluates true when conditional statement is false && true 如果我的函数评估为true,如何使我的Confirm语句过帐状态;如果为false,如何取消? - How can I make my confirm statement post a status if my function evaluates as true, or cancel if false? 如何先执行一个Promise,然后再转到JavaScript中的下一条语句 - How to execute a promise first and then move to the next statement in JavaScript jQuery if语句始终评估为true - jQuery if statement always evaluates to true 尽管为真,但如果语句评估为假 - If statement evaluates to false despite being true Javascript表达式`[“ Java”,“ Python”,“ Javascript”] [Symbol.iterator]()。next()。value`计算为给定数组的第一个值 - Javascript expression `[“Java”, “Python”,“Javascript”][Symbol.iterator]().next().value` evaluates to first value of a given array 在所有情况下,第一个条件的条件评估为True - Condition evaluates as True on first condition in all cases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM