【问题标题】:MPG Text field isn't updating with Javascript getElementByIdMPG 文本字段未使用 Javascript getElementById 更新
【发布时间】:2014-10-02 16:33:37
【问题描述】:

我无法将 var mpg total 输入“每加仑英里数”文本字段。它与 var $ 一起使用,但我试图摆脱捷径。我在 Stackoverflow 或 Google 上找不到答案。有人能看出我哪里出错了吗?

<script>
    var $ = function (id) {
        return document.getElementById(id);
    }                           

    var calculateMpg = function () {            
        var miles = parseFloat($("miles").value); 
          var gallons = parseFloat($("gallons").value); 

        if (isNaN(miles) || isNaN(gallons)) {    
            alert("Both entries must be numeric"); 
        }
        else {                      
            var mpg = miles / gallons;       
            function (id) {
                return (document.getElementById("mpg").value).toFixed(3); 
            }
        }
    }
    window.onload = function () {       
        $("calculate").onclick = calculateMpg;  
        $("gallons").focus();         
    }
</script>

</head>

<body>
<section>
    <h1>Calculate Miles Per Gallon</h1>
    <label for="miles">Miles Driven:</label>
    <input type="text" id="miles"><br>

    <label for="gallons">Gallons of Gas Used:</label>
    <input type="text" id="gallons"><br>

    <label for="mpg">Miles Per Gallon</label>
    <input type="text" id="mpg" disabled><br>

    <label>&nbsp;</label>
    <input type="button" id="calculate" value="Calculate MPG"><br>
</section>

【问题讨论】:

    标签: javascript textbox textfield


    【解决方案1】:

    要让上面的代码在文本字段中显示mpg:

    <script>
        var $ = function (id) {
            return document.getElementById(id);
        }                           
    
        var calculateMpg = function () {            
            var miles = parseFloat($("miles").value); 
              var gallons = parseFloat($("gallons").value); 
    
            if (isNaN(miles) || isNaN(gallons)) {    
                alert("Both entries must be numeric"); 
            }
            else {                      
                var mpg = miles / gallons;       
                document.getElementById("mpg").value = mpg.toFixed(3);
                //function (id) {
                //    return (document.getElementById("mpg").value).toFixed(3); 
                //}
            }
        }
        window.onload = function () {       
            $("calculate").onclick = calculateMpg;  
            $("gallons").focus();         
        }
    </script>
    

    在被注释掉的代码中:不可能像这样定义匿名函数。即使有可能,该函数也不会在任何地方被调用,因此 mpg 字段永远不会被更新。

    【讨论】:

      【解决方案2】:

      您正在尝试更改“mpg”字段的值,但您没有告诉它您想要的值是什么:.value() 只会返回当前值,.value(mpg) 会尝试设置值。你需要这样的东西:

      var set_mpg_field = function(mpg){
        var mpg_field = document.getElementById("mpg");
        mpg_field.value(mpg.toFixed(3));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-29
        • 2014-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-30
        • 2014-11-24
        • 2020-08-19
        相关资源
        最近更新 更多