javascript - 输入字段未更新

标签 javascript html

我正在尝试使用 HTML 和 Javascript 创建一个小费计算器,每次用户更改餐费和小费金额的输入字段时,我都必须验证它是否是数字,如果是数字,我有将数字减少到小数点后两位。

   <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;"/>

validateMealCostvalidateTipPercent 函数缺少将值转换为数字的 parseInt,而 calculateTipAmount 函数缺少 .value,将其转换为 NaN。

关于javascript - 输入字段未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36189760/

相关文章:

html - 即使链接 href 已完成,将 HTML 链接到 CSS 时也会出现问题,文件位于同一文件夹中

javascript - 如何将输入的文本分组到数组中并将每 20 个字符包装在一个 div 中

python - 使用 Beautifulsoup 的类的正则表达式

javascript - 使用 Jqplot 和 Sqlite 数据库中的数据绘制图形

javascript - 为什么 document.documentElement.clientWidth 和 clientHeight 使我的 Canvas 为 300 像素 x 150 像素?

javascript - 动画双箭头 - CSS

javascript - 如何添加顶部填充以平滑滚动

javascript - window.getComputedStyle 不适用于除 Chrome 之外的其他浏览器中的速记属性

javascript - jQuery 表单验证。如何防止错误消息多次出现

javascript - 将 'Blade' 附加到没有 'popping-down' 现有容器的容器中