【发布时间】:2015-10-16 22:33:00
【问题描述】:
我正在尝试从文本表单中获取输入(特别是数字/浮点数),并将其分配给我可以用来进行计算的变量。单击计算按钮时应将其分配给变量。
我正在使用 getElementById(),它为我返回 undefined。
之前的研究指出,我需要将我的脚本标签放在我的输入标签下方。
所以我将脚本从 head 标记移到了 body 标记,但我仍然遇到问题(仍然返回未定义)。
<form name = "myform">
Item Price <input type = "text" name = "tPrice">
<br><br>
<input type = "button" value = "Calculate" onClick = "calculate()"/>
<input type = "submit" value = "Submit"/>
<input type = "reset" value = "Clear"/>
</form>
<script>
var totalPrice = parseFloat(document.getElementById("tPrice").value);
//var totalPrice = document.getElementById("iPrice");
//var price = document.getElementById("price").value;
//document.getElementById("price").value = totalPrice;
//var shipMeth = document.getElementById("ship").checked;
function calculate()
{
//DISPLAY Results
window.alert(totalPrice);
//shipping and handling results
document.myform.sh.value = "500";
//tax
document.myform.tax.value = "500";
//total
document.myform.total.value = "500";
//window.alert(document.myform.sh.value);
window.alert("Results function successful")
return true;
}
</script>
【问题讨论】:
标签: javascript html