簡體   English   中英

javascript if語句返回值

[英]javascript if statement return value

我正在嘗試從一個名為ValuationChoice(標記為Job Value)的html輸入框中獲取一個數值,並將其傳遞給Javascript函數(GetPermit_Price),在Javascript函數中,該函數將通過遍歷嵌入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;

因此,首先要輸入的內容應該是type=number為什么也不能像下面這樣找到輸入的值

const Valuation = document.getElementById('ValuationChoice').value;

並確保只是控制台日志您得到什么

console.log(Valuation);

這將幫助您調試並查看您是否還獲得了一個數字,如果您使用文本輸入,您可能不會得到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM