简体   繁体   English

带有下拉菜单的HTML / Javascript计算器

[英]HTML/Javascript calculator with dropdown

       function calculate() {
            try
            {
            var value1 = parseInt(validate(document.getElementById("value1").value.trim()));
            var value2 = parseInt(validate(document.getElementById("value2").value.trim()));
            document.getElementById("result").value = operate(value1, value2);
            }
            catch (err) {
                alert("There was a problem: " + err.messaqge);
            }
        }
            catch (err) {
                alert("There was a problem: " + err.messaqge);
            }
        }

        function operate(value1, value2) {
            if (operator == addition) {
                return value1 + value2;
            } else if (operator == subtraction) {
                return value1 - value2;
            } else if (operator == division) {
                return value1 / value2;
            } else if (operator == multiplication) {
                return value1 * value2;
            }

            function validate(value) {
                if (value == null || value == "") {
                    alert("Required Field");
                    return 0;
                } else if (isNaN(value)) {
                    alert("Must be a Number Field");
                    return 0;
                } else return value;
            }
        }
    </script>
    <title>Title Bar</title>
  </head>
  <body>
      <h2>Calculator</h2>
      <table border="1">
          <tr>
            <td>Value 1: </td>
            <td><input type="text" id="value1" name="value1"/></td>
          </tr>
          <tr>
            <td>Value 2: </td>
            <td><input type="text" id="value2" name="value2" /></td>
          </tr>
          <tr>
            <td>Operator: </td>
            <td><select id="operator">
            <option value="Add">+</option>
            <option value="Subtract">-</option>
            <option value="Multiply">*</option>
            <option value="Divide">/</option>
            </select></td>
          </tr>
          <tr>
            <td>Result: </td>
            <td><input type="text" id="result" name="result" /></td>
          </tr>
      </table>
      <input type="submit" id="btn_sub" name="btn_sub" value="Submit" onclick="calculate()" />
      <input type="reset" id="btn_res" name="btn_res" value="Reset" />
  </body>
</html>

This is for a really basic calculator program. 这是一个非常基本的计算器程序。 I had it as just addition and it was working, but I am trying to add a dropdown so I can do different operations, and I'm not able to get this to work. 我只是将其作为附加项而已,但是它正在运行,但是我试图添加一个下拉列表,以便可以执行不同的操作,而我无法使其正常工作。 It's gotta be just the operate function or the 3rd line in the calculate function, because everything else was working when it was just for addition. 它只能是操作功能或计算功能中的第三行,因为其他所有功能在添加时都在起作用。

You can get the value of the drop down using: 您可以使用以下方法获得下拉菜单的值:

var operator = document.getElementById('operator').value;

Check this example in which I take the operator value and then pass it to operate 检查这个例子中,我采取了运营商值,然后把它传递给operate

http://jsfiddle.net/wE77s/2/ http://jsfiddle.net/wE77s/2/

http://www.echoecho.com/htmlforms11.htm http://www.echoecho.com/htmlforms11.htm

You are looking for a Drop Down Menu. 您正在寻找下拉菜单。 Once you have that in your java script code see what math symbol is selected in the drop down and perform the calculation. 在Java脚本代码中找到它后,请在下拉列表中选择要选择的数学符号并执行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM