简体   繁体   English

使用 if 语句的错误消息不起作用(Java Script)

[英]error message using if statement doesn't work (Java Script)

The error messages should be displayed in the span elements to the right of the text boxes, and the error messages should be:错误消息应显示在文本框右侧的 span 元素中,错误消息应为:

"Must be a positive number less than $10,000" “必须是小于 10,000 美元的正数”

"Must be a positive number less than 12" “必须是小于 12 的正数”

But my code only displays "Must be a positive number less than $10,000" which is subtotal_message.但我的代码只显示“必须是小于 10,000 美元的正数”,即 subtotal_message。

How can I make both messages show up when its condition is not satisfied?当条件不满足时,如何使两条消息都显示?

 if (isNaN(subtotal) || subtotal <= 0 || subtotal > 10000) {
          $("subtotal_message").innerHTML = "Must be a positive number less than $10,000"; 
      } 

 else if (isNaN(taxRate) || taxRate <= 0 || taxRate > 12) {
          $("tax_rate_message").innerHTML = "Must be a positve number less than 12";
      } 

 else { var salesTax = calculate_salesTax(subtotal, taxRate);
          $("sales_tax").value = calculate_salesTax(subtotal, taxRate);
          $("total").value = (parseFloat($("subtotal").value) + parseFloat(salesTax)).toFixed(2);
      }

HTML within body tag正文标签中的 HTML

    <div id="taxCalc">
    <label for="subtotal">Subtotal:</label>
    <input type="text" id="subtotal" >
    <span id="subtotal_message" class="subtotal_message">Enter order subtotal</span><br />

    <label for="tax_rate">Tax Rate:</label>
    <input type="text" id="tax_rate" >
    <span id="tax_rate_message" class="tax_rate_message">Enter sales tax rate (99.9)</span><br />
    </div>

That's because you're using else if here.那是因为你在这里使用else if If one of the if conditions execute, the others won't execute.如果if条件之一执行,则其他条件将不会执行。 I suggest you remove else if and replace it with if .我建议您删除else if并将其替换为if

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

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