2017-04-02 20 views
0

テキストフィールドで、値を指定してボタンで数えます。私はなぜそんなに考えないのか分からないのですか?あなたはスイッチを介して、原則的にそれを実装する必要がありますが、操作はカウントされない理由を私は理解できません:+、 - 、*、/JavaScriptの電卓(簡易)

<p> 
    <label for="text1">FirstNum</label> 
    <input type="text" id="text1"><br> 
    <label for="operation">Operation</label> 
    <input type="text" id="operation"><br> 
    <label for="text2">SecondNum</label> 
    <input type="text" id="text2"><br> 
    <label for="text3">Result</label> 
    <input type="text" id="text3"><br><br> 
    <input type="button" value="ClickResult" onclick="Calc()"> 
</p> 

<script> 

    function Calc(operation) { 
    //var op; 
    switch(operation) { 
     case '+': 
     text3.value = text1.value + text2.value; 
     break; 
     case '-': 
     text3.value = text1.value - text2.value; 
     break; 
     case '/': 
     text3.value = text1.value/text2.value; 
     break; 
     case '*': 
     text3.value = text1.value * text2.value; 
     break; 
    }  
} 

</script> 
+0

1.私はあなたが私はそれが不明で見つけることだとして、あなたの質問を明確にする必要があると思います。 2. Calcへの入力例を示します。 3. 'value'フィールドは通常は文字列です。 – Carcigenicate

答えて

0

あなたがそうのようなテキストボックスの値を取得する必要があります。

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

しかし、あなたが得る値は、あなたがint型にそれを型キャストかそこらのようにフロートする必要がありますのでintやfloat型に変換する必要があるので、文字列の形式になります。その後、

text1 = parseFloat(text1); 

とで値を配ります結果ボックスは好きです

document.getElementById('text3').value = result of the calculation 

または別の方法がある:eはそう

var result = document.getElementById('text3'); 
result.value = result of computation 

<p> 
 
    <label for="text1">FirstNum</label> 
 
    <input type="text" id="text1"><br> 
 
    <label for="operation">Operation</label> 
 
    <input type="text" id="operation"><br> 
 
    <label for="text2">SecondNum</label> 
 
    <input type="text" id="text2"><br> 
 
    <label for="text3">Result</label> 
 
    <input type="text" id="text3"><br><br> 
 
    <input type="button" value="ClickResult" onclick="Calc()"> 
 
</p> 
 
     
 
<script> 
 

 
    function Calc() { 
 
    var text1 = document.getElementById('text1').value; 
 
    var text2 = document.getElementById('text2').value; 
 
    var operation = document.getElementById('operation').value; 
 
    var text3 = document.getElementById('text3'); 
 
    text1 = parseFloat(text1); 
 
    text2 = parseFloat(text2); 
 
    //var op; 
 
    switch(operation) { 
 
     case '+': 
 
     text3.value = text1 + text2; 
 
     break; 
 
     case '-': 
 
     text3.value = text1 - text2; 
 
     break; 
 
     case '/': 
 
     text3.value = text1/text2; 
 
     break; 
 
     case '*': 
 
     text3.value = text1 * text2; 
 
     break; 
 
    } 
 

 
    } 
 
    
 
</script>

+0

単項演算子 '+'を前置するだけで文字列を強制的に強制することができるときに 'parseFloat()'を実行する必要はありませんので、 'text1 = parseFloat(text1) 'は' text1 = + text1'になります。一般に、JavaScriptの数字は[IEEE 754](http://en.wikipedia.org/wiki/IEEE_754)の標準に従っているので、 'parseInt()'や 'parseFloat()'は避けることをお勧めします。 'parseInt()'と 'parseFloat()'にはユースケースがありますが、それらのどれかではありません。 – spex

+0

@spex情報ありがとうございました、あなたかもしれません、私は+ text1と+ text2にparseFloatをやり直します –