簡體   English   中英

如何使用HTML輸入框設置JavaScript變量?

[英]How do I use a HTML input box to set a JavaScript variable?

我試圖用用戶輸入編寫一個簡單的一頁計算器腳本。 由於缺乏JS知識,我不斷遇到問題。 我覺得我正在做的事情應該可行並且是正確的,但是顯然我在這里缺少一些東西。

我希望HTML輸入框為JS函數設置變量。 我只是無法弄清楚我在做什么錯。

如果我能朝着正確的方向前進,我將不勝感激。

 <body> <input type="text" id="price"=>Price</input> <br> <input type="text" id="value"=>Value</input> <br> <button onclick="sub()">Submit</button> </body> <script> function sub() { var value = document.getElementById("value").value; var price = document.getElementById("price").value; var keep = value * 0.7 var sell = keep * 0.7 var profit = sell / 1000 * 6 - price var keeprate = price / keep * 1000 var earned = profit + price document.write("Keeping: R$ "); document.write(keep); document.write("<br>"); document.write("1k Buy Rate: $"); document.write(keeprate); document.write("<br>"); document.write("<br>"); document.write("Selling: R$ "); document.write(sell); document.write("<br>"); document.write("1k Buy Rate: "); document.write("$6.00"); document.write("<br>"); document.write("<br>"); document.write("Total Cost $"); document.write(price) document.write("<br>"); document.write("Total Return $"); document.write(earned) document.write("<br>"); document.write("Net: $"); document.write(profit); } </script> 

我認為您的主要問題是您的函數聲明。 您沒有function sub()開合括號

添加一個開始和結束括號以正確聲明您的功能。

function sub() {

    code

}

暫無
暫無

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

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