1

这是我的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The Raptor Astronomer</title>
        <script>
          function getQuantity()
{
    var theForm = document.forms["formula"];
    var radiusform = theForm.elements["radius"];
    var radius =0;
    if(radiusform.value!="")
    {
        radius = parseInt(radiusform.value);
          var circumference = radius * 2 * Math.PI;
    }
theForm.elements["circumference"].value = var circumference;
}
getQuantity();

    </script>
    </head>
    <body>
        <form id="formula">
            <label>Radius</label>
            <input id="radius" name="radius" type="text" />
            <label>Circumference</label>
            <input id="circumference" name="circumference" type="text"/>
        </form>

    </body>
</html>

据我从教程中可以看出,我的代码应该更新为圆周 - 不过我没有看到这种情况发生。

为什么这不能正常运行?我不包括触发器吗?需要触发吗?

4

2 回答 2

0

删除var:-

theForm.elements["circumference"].value = circumference;

并在之前声明if

var circumference=0
if(radiusform.value!="")
    {
        radius = parseInt(radiusform.value);
        circumference = radius * 2 * Math.PI;
    }
于 2012-09-02T00:53:39.713 回答
0

做了一些小的改动。请参阅jsFiddle 示例。我将调用脚本绑定到半径输入元素的 onchange 事件。您也不能将 var 用作作业的一部分,因此我将其移至正确的位置。

JavaScript

function getQuantity() {
    var theForm = document.forms["formula"];
    var radiusform = theForm.elements["radius"];
    var radius = 0;
    var circumference;
    if (radiusform.value != "") {
        radius = parseInt(radiusform.value);
        circumference = radius * 2 * Math.PI;
    }
    theForm.elements["circumference"].value = circumference;
}

​<strong>HTML

 <form id="formula">
            <label>Radius</label>
            <input id="radius" name="radius" type="text" onChange="getQuantity()"/>
            <label>Circumference</label>
            <input id="circumference" name="circumference" type="text"/>
        </form>​
于 2012-09-02T00:57:59.363 回答