2014-10-11 3 views
0

나는 온라인 계산기를 만들고 싶다. HTML에서 폼을 사용할 수 있다고 생각했지만, 약간의 폼을 만들었지 만 약간의 문제가있다. (입력 타입 = "number"name = "first number")와 (input type = "number"name = "second number")를 입력하여 스크립트 방정식에 넣은 다음 사용자 파일 번호를 입력하고 (input type = "text" http://jsfiddle.net/nco5r74k/HTML과 자바 스크립트를 사용하여 온라인 계산기를 만드는 것

다음
<pre> 
<!DOCTYPE html> 
<html lang="cs-CZ"> 
    <head> 
     <meta charset="UTF-8"> 
     <meta name="generator" content="Prace"> 
     <title>kalkulačka</title> 
    </head> 
    <body>   
     <form> 
     <label for="vypln_cislo1">zadej zde první číslo</label> <input type="number" name="prvni_cislo" min="0" max="10000"> 
     <select name="znamenko"> 
     <option value="+">+</option> 
     <option value="-">-</option> 
     <option value="x">x</option> 
     <option value=":">:</option> 
     </select> 
     <label for="vypln_cislo2">zadej zde druhé číslo</label> <input type="number" name="druhe" min="0" max="10000"> 
     <input type="submit" value="vypočítat"> 
     <label for="vysledek">výsledek:</label> <input type="text" size="15" name="vysledek" readonly> 
     </form> 
     <script> 
      var a, text; 
      y = 
      x = 
      a = y + x 
      text = + a; 
      document.write(text); 
     </script> 
     <script> 
      var b, text; 
      y = 
      x = 
      b = y - x 
      text = + b; 
      document.write(text); 
     </script> 
     <script> 
      var c, text; 
      y = 
      x = 
      c = y * x 
      text = + c; 
      document.write(text); 
     </script> 
     <script> 
      var d, text; 
      y = 
      x = 
      d = y/x 
      text = + d; 
      document.write(text); 
     </script> 
    </body> 
</html> 
</pre> 

답변

1

당신이 input 요소에서 읽고에서 onclick 이벤트 처리기를 사용하여 span 요소에 쓸 document.getElementById()을 사용하는 방법의 작은 예제 버튼 제출 button.

A: <input id="a" value="3"/><br/> 
 
B: <input id="b" value="5"/><br/> 
 
<button onclick="document.getElementById('product').innerText = document.getElementById('a').value * document.getElementById('b').value;">Calculate</button> 
 
<br/> 
 
A * B = <span id="product"></span>

0

은 정말 귀하의 질문이나 당신이 일을하려고하는 이해가 안 돼요.

그러나 귀하의 입력 필드의 데이터를 얻을 당신은 DOM 액세스를 수행해야합니다

var druhe = document.getElementsByTagName('druhe')[0].value; 

을 그리고 당신은 다시로를 작성해야 : 어쩌면 먼저 읽어야

document.getElementsByTagName('vysledek')[0].value = "test1234"; 

자바 스크립트 돔 자습서. 예를 들면입니다.

안부

더스틴

0

내가 당신을 도울 수있는 자바 스크립 eval.This 접근 방식을 사용합니다 간단한 계산기를 개발했다.

온라인으로 테스트 할 수 있습니다.

http://luckyalgo.blogspot.in/2014/12/calculator-using-java-script.html

<code> 
<!DOCTYPE html> 
<html> 
<head> 
<style> 
table, th, td { 
    border: 1px solid black; 
    width: 130px; 
} 

#CLEAR { 
    width: 150px; 
} 

button { 
    width: 45px; 
} 
</style> 
<script> 
function myFunction(clickedId) { 
    document.calc.result.value+=clickedId; 
} 
function Clear() { 
    document.calc.result.value=""; 
} 
function compute() { 
    try{ 
    var inp=eval(document.calc.result.value); 
    document.calc.result.value=inp; 
    }catch(err){ 
      document.calc.result.value="Bad Input!!"; 
    } 
} 
</script> 
</head> 
<body> 
    <form name="calc"> 
    <input type="text" name="result" readonly> 
    </form> 

<table > 
    <tr> 
    <td><button type="button" id="1" onclick="myFunction(this.id)">1</button></td> 
    <td><button type="button" id="2" onclick="myFunction(this.id)">2</button></td> 
    <td><button type="button" id="3" onclick="myFunction(this.id)">3</button></td> 
    </tr> 
    <tr> 
    <td><button type="button" id="4" onclick="myFunction(this.id)">4</button></td> 
    <td><button type="button" id="5" onclick="myFunction(this.id)">5</button></td> 
    <td><button type="button" id="6" onclick="myFunction(this.id)">6</button></td> 
    </tr> 
    <tr> 
    <td><button type="button" id="7" onclick="myFunction(this.id)">7</button></td> 
    <td><button type="button" id="8" onclick="myFunction(this.id)">8</button></td> 
    <td><button type="button" id="9" onclick="myFunction(this.id)">9</button></td> 
    </tr> 
    <tr> 
    <td><button type="button" id="0" onclick="myFunction(this.id)">0</button></td> 
    <td><button type="button" id="+" onclick="myFunction(this.id)">+</button></td> 
    <td><button type="button" id="-" onclick="myFunction(this.id)">-</button></td> 
    </tr> 
    <tr> 
    <td><button type="button" id="*" onclick="myFunction(this.id)">*</button></td> 
    <td><button type="button" id="/" onclick="myFunction(this.id)">/</button></td> 
    <td><button type="button" id="ANS" onclick="compute()">ANS</button></td> 
    </tr> 
</table> 

<button type="button" id="CLEAR" onclick="Clear()">CLEAR</button> 

</body></html> 


</code> 
+0

당신이이 경우 (버튼의 값을 결정하는 대신에'id' 속성의'데이터-'속성을 사용하지 말아야합니다, 나는 심지어 요소의 값을 볼 수 있었다 대신 this.innerText'가 될 것입니다)? 나는'id' 속성을 사용하는 것이 좋지 않다고 생각합니다. –

+0

좋은 지적입니다. 나는 그것을 시도 할 것이다. 나는 자바 스크립트에 새로운 것이므로 단지 뭔가를 얻고 싶었다. –

관련 문제