【问题标题】:second if statement not working第二个 if 语句不起作用
【发布时间】:2018-09-26 03:56:04
【问题描述】:

在下面的程序中,我使用了 3 个 if 条件,但中间的 if 语句不起作用,最后一个 if 语句工作正常,当我再次将第三个 if 语句更改为第二个语句时,第二个语句无法正常工作。 第三个 if 语句工作正常

function calculate() {
    var quantity = document.getElementById("quantity").value;
    var price = document.getElementById("price").value;
    var discount = document.getElementById("discount").value;
    var tax = document.getElementById("tax").value;

    if((quantity && price ) != null) {
        amount = quantity * price;
        document.getElementById("amount").value = amount;
    } else {
        alert("price & tax required");
    }

    if(discount != null) {
        var discount_amount = (quantity*price*discount)/100;
        amount = (quantity*price) - discount_amount;
        document.getElementById("amount").value = amount;
    } else {
        document.getElementById("amount").value = quantity * price;
    }

    if(tax != null) {
        var tax_amount = (quantity*price*tax)/100;
        amount = (quantity*price) + tax_amount;
        document.getElementById("amount").value = amount;
    } else {
        document.getElementById("amount").value = quantity * price;
    }
}

【问题讨论】:

  • 有什么问题?比如,什么不起作用。它应该做什么,它没有做什么? js 控制台说什么?
  • 你能把你的 HTML 也放在你的问题中吗?
  • 尝试 document.write(discount) 以查看折扣值以确保它不为空
  • @StuiterSlurf 请不要建议document.write(),因为它会在擦除页面内容时混淆问题。 console.log() 非常适合快速调试。我什至更喜欢alert() 而不是document.write()!!!
  • 就像 Nina 指出的,Javascript 中的字符串是字符串,null 是不同的类型。空字符串等于"",不为空。但是,如果您确实希望空字符串为空,您也可以这样做 -> var quantity = document.getElementById("quantity").value || null

标签: javascript


【解决方案1】:

input.value 返回一个字符串,它永远不会是null,但它可以是一个空字符串,例如''

空字符串 a 在数字上下文中转换为零。如果不需要,您需要一个明确的检查,例如

if (input.value === '') {
    return;
}

【讨论】:

    【解决方案2】:
    if(discount != null)
    

    discount 将是空字符串而不是空检查:

     if(discount !== "")
    

    【讨论】:

      【解决方案3】:

      您无需在任何 if 测试中显式检查 null 值,如零值以及未定义

      if (quantity && price) {
         // only want to come in here for non-zero values of both
      }
      
      if (discount) {
         // only interested in the discount for non-zero values
      }
      
      if (tax) {
        // only interested in non-zero values of tax
      }
      

      【讨论】:

        【解决方案4】:

        如果(折扣!= null)

        假设折扣变量是空字符串。你觉得结果怎么样?

        if(""!=null)
        

        它将是true 如果您的字符串包含一些数据怎么办

        if("someData"!=null)
        

        又是true

        我不怪你。在这种情况下,Javascript 有一些神奇的作用。

        在javascript中,有很多逻辑操作可以表示为false for if条件。下面的代码,所有if 语句都返回false

        if(""){//code here}
        if(null){//code here}
        if(0){//code here}
        

        开发人员不应比较nullstring 等两种不同的类型。 以防万一,我建议您避免使用双重等号==。使用三重 === 它是 type-sensitive 。 见here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-01
          • 1970-01-01
          • 2016-01-03
          • 2012-07-16
          • 2021-04-23
          • 1970-01-01
          相关资源
          最近更新 更多