找到两个值中的较大者,然后打印出来,这为什么不起作用?

时间:2019-12-13 19:52:45

标签: javascript

我试图找到两个值中的较大者,然后将其显示在id为“ totalcost”的输入中

我希望显示“ volume”的值或“ actualWeight”的值,但它返回的是“ undefined”。

我缺少什么和/或做错了什么?

function calculateVolume() {
  var width = +document.getElementById("width").value;
  var height = +document.getElementById("height").value;
  var length = +document.getElementById("length").value;
  var volume = (width * height * length) / 5000;
  var volume = volume.toFixed(1);
  var volume = Math.ceil(volume * 2) / 2;
  document.getElementById("volume").value = volume;
}

function calculateVolumeLBS() {
  var widthlbs = +document.getElementById("widthlbs").value;
  var heightlbs = +document.getElementById("heightlbs").value;
  var lengthlbs = +document.getElementById("lengthlbs").value;
  var volumelbs = (widthlbs * heightlbs * lengthlbs) / 138.4;
  var volumelbs = volumelbs.toFixed(1);
  var volumelbs = Math.ceil(volumelbs * 2) / 2;
  document.getElementById("volumelbs").value = volumelbs;
}

function calculateTotal() {
  calculateVolume();
  calculateVolumeLBS();

  var billedWeight = (function() {
    if (volume > actualWeight)
      return volume;
    else if (volume === actualWeight)
      return actualWeight;
    else
      return actualWeight;
  })();
  document.getElementById("totalcost").value = billedWeight;
}
<div>
  <div style="width:24%; float:left;">
    <label> 
             <input type="radio" name="colorRadio" 
              value="Inches" checked> Inches</label>
  </div>
  <div style="width:74%; float:left;">
    <label> 
      		<input type="radio" name="colorRadio" 
              value="Centimeters"> Centimeters</label>
  </div>
</div>
<br>
<div style="width:24%;float:left;">
  <p>Length</p>
  <p>Width</p>
  <p>Height</p>
  <br>
  <p>Actual Weight</p>
  <p>Volumetric Weight</p>
  <br>
  <p>Total Cost*</p>
</div>
<div class="Centimeters selectt" style="width:74%; float:left;">
  <div>
    <p><input type="Text" id="length" name="length" size="4" onkeyup=""> cm</p>
    <p><input type="Text" name="width" id="width" size="4"> cm</p>
    <p><input type="Text" id="height" name="height" size="4" onkeyup=""> cm</p>
    <br>
    <p><input type="Text" name="actualWeight" id="actualWeight" size="4"> kg</p>
    <p><input type="Text" name="volume" id="volume" size="4" readonly="readonly" style="border:none;"> kg</p>
    <br>
    <p>$<input type="Text" name="totalcost" id="totalcost" size="4" readonly="readonly" style="border:none;"></p>
  </div>
  <br>
  <p><input type="button" name="calculate" id="volume" size="4" onclick="calculateTotal()" value="Calculate"></p>
</div>
<div class="Inches selectt" style="width:74%; float:left;">
  <div>
    <p><input type="Text" id="lengthlbs" name="lengthlbs" size="4" onkeyup=""> in</p>
    <p><input type="Text" name="widthlbs" id="widthlbs" size="4"> in</p>
    <p><input type="Text" id="heightlbs" name="heightlbs" size="4" onkeyup=""> in</p>
    <br>
    <p><input type="Text" name="actualWeight" id="actualWeight" size="4"> lbs</p>
    <p><input name="volumelbs" id="volumelbs" size="4" readonly="readonly" style="border:none;"> lbs</p>
    <br>
    <p>$<input type="Text" name="totalcost" id="totalcost" size="4" readonly="readonly" style="border:none;"></p>
  </div>
  <br>
  <p><input type="button" name="calculate" id="volumelbs" size="4" onclick="calculateTotal()" value="Calculate"></p>
</div>

0 个答案:

没有答案