需要帮助从表单输入分配变量
Need help assigning variable from form input
我试图从文本形式获得输入(特别是数字/浮点数),并将其分配给一个变量,我可以用它来做计算。当计算按钮被点击时,它应该被赋值给变量。
我正在使用getElementById(),它为我返回未定义。
先前的研究指出,我需要在我的输入标签下面有我的脚本标签。
所以我把我的脚本从头部标签移动到正文标签,我仍然有问题(仍然得到未定义的返回)。
<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>
Input需要一个ID属性,您可能希望将值的获取和解析移到calculate函数中。
Item Price <input type = "text" id = "tPrice">
和
function calculate()
{
var totalPrice = parseFloat(document.getElementById("tPrice").value);
//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;
}
问题是当输入没有值时,获取值在页面加载时运行。如果您只在单击按钮后获取值,则在尝试获取它之前,请确保输入已经设置了值。
您需要设置input id="tPrice"
而不是name
属性。希望有帮助
变化
Item Price <input type = "text" name = "tPrice">
Item Price <input type = "text" id = "tPrice">
我会将getElementById调用移动到计算方法中,因为如果它是一个文本输入,您应该仅在用户用值填充它之后才计算其值。是的,在输入中添加id而不是name属性。如果你想在页面加载时立即保存输入的值,那么你应该等待DOM准备好:
$(document).ready(function() { /* code here */ });
并且要小心,因为如果值为空,parseFloat将产生NaN
将var
放入函数中,如下所示:
function calculate()
{
var totalPrice = parseFloat(document.getElementById("tPrice").value);
//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;
}
然后像这样将id="tPrice"
添加到输入中:
<input type = "text" name = "tPrice" id="tPrice">
参见:http://jsfiddle.net/vantbrhb/8/
相关文章:
- 为什么当CRLF被分配到<输入>使用php
- 如何通过javascript为输入类型日期分配默认值
- 通过参数将传递的函数分配给动态创建的输入类型的onclick事件
- 从Javascript中的循环提示输入中分配多个变量
- 在切换情况下未分配输入
- javascript的新手,如何为提交按钮分配输入值
- 如何为每个无线电输入组分配不同的名称
- 将用户输入分配给索引中带有变量的对象
- jsp-如何将输入值分配给我想稍后使用的另一个变量
- Javascript:为在MS Edge中不起作用的其他框架中的输入框分配值
- 将多个图像(文件)从一个文件输入分配给另一个文件输入 - 上传
- 如何将 javascript 变量分配给隐藏的输入
- 使用eval使用字符串输入按名称将函数分配给变量有什么风险
- 仅当输入字段中有内容时才使用 javascript 分配类
- JavaScript.为字母分配数字值,并根据用户的字母输入执行计算
- 如何根据条件在输入上分配 ng 模型中的变量
- 如何使用拼接方法将输入的单词与提示分开并将它们分配给变量
- JavaScript - 如何将用户输入值分配给脚本中的变量
- 需要帮助从表单输入分配变量
- 中继器“onclick”中的 LinkButton 为隐藏的输入分配一个值