javascript if语句返回值

标签 javascript html

我正在尝试从名为 ValuationChoice(标记为 Job Value)的 html 输入框中获取数值,并将其传递给 Javascript 函数(GetPermit_Price),在该函数中它将通过循环遍历嵌入到 ValuationChoice 中的值来获取许可成本。无论我在 ValuationChoice 输入框中键入什么,结果值都会一直显示为 0。任何关于我如何完成这项工作的建议都将不胜感激。提前致谢!

function GetPermit_Price() {

  var PermitCost = 0;
  var theForm = document.forms["cakeform"];
  var Valuation =
    theForm.elements["ValuationChoice"];

  if (Valuation > 0 && Valuation <= 1000) {
    PermitCost = 0;
  }

  if (Valuation > 1001 && Valuation <= 50000) {
    PermitCost = (((Valuation - 1000) / 1000) * 5.50) + 25;
  }

  if (Valuation > 50001 && Valuation <= 100000) {
    PermitCost = (((Valuation - 50000) / 1000) * 4.50) + 294.50;
  }

  if (Valuation > 100001 && Valuation < 500000) {
    PermitCost = (((Valuation - 100000) / 1000) * 3.50) + 519.50;
  }

  if (Valuation > 500001) {
    PermitCost = (((Valuation - 500000) / 1000) * 2.25) + 1919.50;
  }

  return PermitCost;
}

function calculateTotal() {

  var cakePrice = GetPermit_Price() + 5 + 10.00;

  var divobj7 = document.getElementById('permitFee');
  divobj7.style.display = 'block';
  divobj7.innerHTML = "Permit Fee: $" +
    GetPermit_Price();
}

function hideTotal() {
  var divobj = document.getElementById('totalPrice');
  divobj.style.display = 'none';
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1- 
    strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Cake Form</title>
  <script type="text/javascript" src="js/formcalculations.js"></script>
  <link href="styles/cakeform.css" rel="stylesheet" type="text/css" />

</head>

<body onload='hideTotal()'>
  <div id="wrap">
    <form action="" id="cakeform" onsubmit="return 
    false;">
      <div>
        <div class="cont_order">
          <fieldset>
            <legend>Calculate Permit</legend>

            <label>Job Value</label>
            <input type="text" id="ValuationChoice" name='ValuationChoice' />

            <p>
            </p>
            <div id="permitFee"></div>
            <br/>

            <br/>
            <div id="totalPrice"></div>

            <input type='submit' id='submit' value='Calculate Permit' onclick="calculateTotal()" />

          </fieldset>
        </div>

      </div>
    </form>
  </div>
  <!--End of wrap-->

</body>

</html>

最佳答案

估值是输入字段而不是它的值。

尝试:

var Valuation = theForm.elements["ValuationChoice"].value;

关于javascript if语句返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53654855/

相关文章:

html - 使用 html 和下拉菜单 html css 的 Img 排序

javascript - 单击更改背景图像不起作用

javascript - 使用变量与获取输入值时 substr() 的不同结果

javascript - Jest 在 react-native 中遇到了意外的标记

javascript - 我正在尝试在 JS 中创建一个函数来搜索 HTML 表中的 <td> 标签,但如果我在其中使用任何 <th></th> 标签,它就不起作用

javascript - 动态创建的html中的jquery倒计时

html - 带有框阴影和相关内容的奇怪的 OSX 视网膜 Chrome 滚动条行为

javascript - jQuery 动态切换

javascript - 获取 $rootScope :inprog error when calling click method of input type file programmatically

javascript - 使用下拉菜单通过 Ajax 实现动态内容