通过从下拉列表中选择值来调用函数

时间:2014-04-10 23:22:26

标签: javascript html5

我有两个下拉菜单选择单位,如英寸,英尺e.t.c和用户所需的有效小数位后的有效数字,用于计算圆的面积和其他属性。 我没有任何编码经验,请不要介意代码。

<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>

0 个答案:

没有答案