计算不同直径单位的圆的面积,圆周e.t.c,并在用户所需的小数点后显示有效数字

时间:2014-04-10 22:11:01

标签: javascript html

//我希望代码一旦我从下拉列表中更改单位,就会显示圆圈的面积和圆周等属性,并且还会显示用户所需的十进制后所需的有效数字.....请不要请注意我的代码只是一个试图掌握语言的新手......

<html>

<select name="rd" id="rd">

<script type="text/javascript">
for(var i=1;i<=10;i++){
    document.write("<option value="+i+">"+i+"</option>");
}
</script>

<script type="text/javascript">
 function computeunit() {
     //obtain user input
     var dia = Number(document.getElementById("dia").value);

     var diaunits = document.getElementById("diaunits").value;
     var rn = document.getElementById("rd").value;

     if (diaunits == "meter") dia /= 1.00;
     if (diaunits == "inches") dia /= 39.3700787;
     if (diaunits == "feet") dia /= 3.28084;
     if (diaunits == "yard") dia /= 1.09361;
     if (diaunits == "cm") dia /= 100;
     if (diaunits == "mm") dia /= 1000;

     var circumf = dia * Math.PI;

     document.getElementById("txtcircumf").innerHTML = circumf;

     var ar = (diaN / 2) * (diaN / 2) * Math.PI;

     document.getElementsById("txtarea").innerHTML = ar;

     var rs = new Number(circumf); // don't know how to include area
     var nrs = rs.toFixed(rn);

     document.getElementById("txtcircumf").value = nrs;
 }
</script>

<pre>

<body>

<p>Enter your diameter: <input type="text" id="diaunits"/>
    <select type="multiple" id="diaunits">
        <option value="metres" selected="selected">metres</option>
        <option value="inches">inches</option>
        <option value="feet">feet</option>
        <option value="yard">yard</option>
        <option value="centimeter">centimeter</option>
        <option value="millimeter">millimeter</option>
    </select>
    </p>
Circumference: <input type="text" id="txtcircumf" />    
    <input type="button" value="Calculate" onclick="computeunit();"/></br>    

Area: <input type="text"  id="txtarea" />
      <input type ="button" value="area" onclick="computeunit();"/></br>

</body>
</pre>

<html>

1 个答案:

答案 0 :(得分:0)

我们曾经将onchange处理程序作为select的属性。现在使用JavaScript附加它可能很时髦,但效果相同。