我正在尝试使用用户输入编写一个简单的一页计算器脚本。由于我半缺乏 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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句