quiz06.html 1.5 KB
<!DOCTYPE html>
<html lang="ko">

<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <title>test</title>
</head>

<body>
  <div>
    <input type="text" id="a">
    <select id="op">
      <option>+</option>
      <option>-</option>
      <option>*</option>
      <option>/</option>
    </select>
    <input type="text" id="b">
  </div>
  <button type="button" id="exec">계산</button>
  <span>result:</span>
  <script type="text/javascript">
    $(document).ready(function() {
      $("button").click(function() {
        var a = parseFloat(Number($("#a").val()));
        var b = parseFloat(Number($("#b").val()));
        if (isNaN(a) == true || isNaN(b) == true) {
          alert("숫자를 입력하세요");
          $("span").html("result = error");
        } else {
          var c = $("#op option:selected").val();
          if (c == "+") {
            var d = a + b;
            $("span").html("result ="+d);
          } else if (c == "-") {
            $("span").html("result ="+d);
            var d = a - b;
          } else if (c == "*") {
            var d = a * b;
            $("span").html("result ="+d);
          } else if (c == "/") {
            if (b == 0) {
              alert("0으로 나눌수 없음");
              $("span").html("result = error");
            } else {
              var d = a / b;
              $("span").html("result ="+d);
            }
          }
        }


      });
    });
  </script>
</body>

</html>