输入字段未更新
Input field not updating
我正试图使用HTML和Javascript创建一个小费计算器,每次用户更改餐费和小费金额的输入字段时,我都必须验证它是否是一个数字,如果是,我必须将数字减少到小数点后2位。
<script>
function validateMealCost(mealCharge){
var mealCost = document.getElementById(mealCharge).value;
if (isNaN(mealCost)){
alert("The cost of the meal has to be a number.");
location.reload();
}
mealCost = mealCost.toFixed(2);
return mealCost;
}
function validateTipPercent(tipPercent){
var tipPercent = document.getElementById(tipPercent).value;
if (isNaN(tipPercent)){
alert("The tip percentage has to be a number.");
location.reload();
}
if (tipPercent >= 1.0){
alert("You are very generous.");
}
tipPercent = tipPercent.toFixed(2);
return tipPercent;
}
function calculateTipAmount(mealCharge, tipPercent){
var tipAmount;
var mealCost = document.getElementById(mealCharge);
var tipPercentage = document.getElementById(tipPercent);
tipAmount = mealCost * tipPercentage;
document.getElementById('tipAmount').value = tipAmount;
}
</script>
<input type="text" id="mealCharge" onchange="validateMealCost('mealCharge');" />
<input type="text" id="tipPercentage" onchange="validateTipPercent('tipPercentage');" />
<button onclick="calculateTipAmount('mealCharge','tipPercentage');">Calculate</button>
<input type="text" id="tipAmount" style="text-align: right;"/>
我不认为它采用了使用toFixed()编辑的值,而且字段tipAmount显示的是NaN。如何修复这些错误?
<script>
function validateMealCost(mealCharge){
var mealCost = document.getElementById(mealCharge).value;
if (isNaN(mealCost)){
alert("The cost of the meal has to be a number.");
location.reload();
}
mealCost = parseInt(mealCost).toFixed(2);
return mealCost;
}
function validateTipPercent(tipPercent){
var tipPercent = document.getElementById(tipPercent).value;
if (isNaN(tipPercent)){
alert("The tip percentage has to be a number.");
location.reload();
}
if (tipPercent >= 1.0){
alert("You are very generous.");
}
tipPercent = parseInt(tipPercent).toFixed(2);
return tipPercent;
}
function calculateTipAmount(mealCharge, tipPercent){
var tipAmount;
var mealCost = document.getElementById(mealCharge).value;
var tipPercentage = document.getElementById(tipPercent).value;
tipAmount = mealCost * tipPercentage;
document.getElementById('tipAmount').value = tipAmount;
}
</script>
<input type="text" id="mealCharge" onchange="validateMealCost('mealCharge');" />
<input type="text" id="tipPercentage" onchange="validateTipPercent('tipPercentage');" />
<button onclick="calculateTipAmount('mealCharge','tipPercentage');">Calculate</button>
<input type="text" id="tipAmount" style="text-align: right;"/>
validateMealCost
和validateTipPercent
函数缺少将值转换为数字的parseInt
,而calculateTipAmount
函数缺少将其转换为NaN的.value
。
您需要解析输入-所有文本输入都将提供字符串,因此不能作为数字与其他数字进行比较-它们不能是那种形式,也不能用于计算。还要注意,即使您使用了一个数字进行计算,使用.toFixed()也会将该数字转换为字符串。
例如,您需要使用parseInt或parseFloat,它们将返回一个数字:
var tipPercent = parseInt(document.getElementById(tipPercent).value);
它不更新,因为您需要在脚本之前声明输入。因此,简单的解决方法是将整个<script>
标签移动到<input>
最后一次出现的位置下方。
您可以使用W3Schools Tryit编辑器模拟结果,并粘贴代码片段:
<!DOCTYPE html>
<html>
<body>
The content of the body element is displayed in your browser.
<input type="text" id="tipAmount" />
<script>
document.getElementById('tipAmount').value = 5*5;
document.getElementById('tipAmount2').value = 5*5;
</script>
<input type="text" id="tipAmount2" />
</body>
</html>
请注意,代码仅更新tipAmount
而不更新tipAmount2
。
相关文章:
- 在visualforce中为更新字段分配HTML链接/按钮
- 反应:以动态生成的形式根据另一个字段更新字段值
- PHP通过AJAX更新字段值
- AngularJS:更改对象不会更新字段,但直接更改字符串会更新字段
- MongoDB:用反值更新字段编号
- MongoDB:将字段移动到新文档并更新字段和数组
- 为什么Javascript对象不更新字段
- 基于计算的自动更新字段
- 如何在流星中自动更新字段
- 我想在 VF 页面上选中复选框时更新字段
- 使用jquery don'更新字段;t更新可观察到的
- 在更改事件jquery上更新字段值
- 如何通过乘以字段值Mongodb来更新字段
- Javascript更新字段的总和
- 使用复选框自动更新字段
- 如何动态更新字段而不刷新和不调用服务器
- 如何使用REST API用新值更新字段
- 当用程序更新字段时,如何在更新时运行jquery函数
- 检查文本框是否已更改.如果是,更新字段
- Jquery,更新字段和启用按钮