jquery 找不到nunber变成NaN的地方?

ffx8fchx  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(103)

我有一个变量,它保存乘法的乘积。在这个乘法中作为因子的两个变量是我已经验证为falseisNaN()的数字。一个变量来自constant,另一个来自用户input
我尝试在这个变量上使用Number()parseFloat(),但都没有帮助。
我在保存产品的div中包含了一个conditional ternary,这样您就可以看到是否返回了一个数字,或者产品是否不是一个数字。
谁能告诉我问题出在哪里,如何纠正?
我已经复制了包含的片段中的问题。

const space_types = {
  option1: {
    space: 'Option 1',
    kwhuse: 9.4,
    kwhcost: 0.92,
    fueluse: 30.8,
    fuelcost: 0.21,
    othuse: 62.7,
    othcost: 1.17
  }
};

const calculate_utilities = {
  electric: {
    total_usage: 0,
    total_cost: 0
  },
  fuel: {
    total_usage: 0,
    total_cost: 0
  },
  electric1: {
    unit_rate: 0,
    unit_cost: 0,
    unit_usage: 0,
    total_usage: 0,
    total_cost: 0
  },
  fuel1: {
    unit_rate: 0,
    unit_cost: 0,
    unit_usage: 0,
    total_usage: 0,
    total_cost: 0
  }
};

$(function() {
  $("#area").trigger('focus');
  $("#area").blur(function() {
    var type = $("#space_type option:selected").val();
     var area = $("#area").val();
   alert("Is 'area' not a number? (JSFiddle returns FALSE) " + isNaN(area))
    calculate_utilities.electric1.unit_cost = space_types[type].kwhcost * area;
    calculate_utilities.electric.unit_cost = calculate_utilities.electric.unit_cost + calculate_utilities.electric1.unit_cost;
    $("#totals").html(
      isNaN(calculate_utilities.electric.unit_cost) ?
      "TRUE: This is not a number" :
      calculate_utilities.electric.unit_cost
    );
  });
});
.test {
  display: flex;
  flex-direction: column;
  width: 26%;
  line-height: 22px;
  font-size: 14px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">
  Space Type:
  <select id="space_type" name="space_type">
    <option value=""></option>
    <option selected value="option1">Option 1</option>
  </select>
  Area:
  <input id="area" type="number" value="100000"> Totals:
  <div id="totals">0</div>
</div>
kcrjzv8t

kcrjzv8t1#

您正在使用属性自身定义中的属性(将已定义的值与未定义的值相加会导致NaN

calculate_utilities.electric.unit_cost = calculate_utilities.electric.unit_cost + calculate_utilities.electric1.unit_cost;

字符串
也许你在这里犯了一个错误?否则,需要在首次定义calculate_utilities时初始化该属性

const space_types = {
  option1: {
    space: 'Option 1',
    kwhuse: 9.4,
    kwhcost: 0.92,
    fueluse: 30.8,
    fuelcost: 0.21,
    othuse: 62.7,
    othcost: 1.17
  }
};

const calculate_utilities = {
  electric: {
    total_usage: 0,
    total_cost: 0,
    unit_cost: 0
  },
  fuel: {
    total_usage: 0,
    total_cost: 0
  },
  electric1: {
    unit_rate: 0,
    unit_cost: 0,
    unit_usage: 0,
    total_usage: 0,
    total_cost: 0
  },
  fuel1: {
    unit_rate: 0,
    unit_cost: 0,
    unit_usage: 0,
    total_usage: 0,
    total_cost: 0
  }
};

$(function() {
  $("#area").trigger('focus');
  $("#area").blur(function() {
    const type = $("#space_type option:selected").val();
    const area = Number($("#area").val());
    calculate_utilities.electric1.unit_cost = space_types[type].kwhcost * area;
    calculate_utilities.electric.unit_cost = calculate_utilities.electric.unit_cost + calculate_utilities.electric1.unit_cost;
    $("#totals").html(
      isNaN(calculate_utilities.electric.unit_cost) ?
      "TRUE: This is not a number" :
      calculate_utilities.electric.unit_cost
    );
  });
});

x

.test {
  display: flex;
  flex-direction: column;
  width: 26%;
  line-height: 22px;
  font-size: 14px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">
  Space Type:
  <select id="space_type" name="space_type">
    <option value=""></option>
    <option selected value="option1">Option 1</option>
  </select>
  Area:
  <input id="area" type="number" value="100000"> Totals:
  <div id="totals">0</div>
</div>

的字符串

相关问题