2017-01-06 33 views
1

電卓は最初からうまく動作しますが、リセットボタンを使うと正常に機能しなくなります。 (注:これをデバッグできるまで、私は乗算を設定しているだけです)。 JavaScriptの使い方を学び始めたところです。どんな助けも素晴らしいだろう。ここで簡単な電卓のリセットボタン

は私のHTMLコードは次のとおりです。

var input = ""; 
 
var firstInput = ""; 
 
var secondInput = ""; 
 
var answer = ""; 
 

 
$(".numbers").click(function(){ 
 
    $("#topBar").text(input += $(this).text()) 
 
}); 
 

 
function operation() { 
 
    firstInput = input; 
 
    $("#topBar").empty(); 
 
    $(".numbers").click(function(){ 
 
    $("#topBar").text(secondInput += $(this).text()) 
 
    }); 
 
} 
 

 
$("#buttonx").click(function(){ 
 
    if(input !== ""){ 
 
    operation(); 
 
    } 
 
}); 
 

 
$("#buttonEqual").click(function(){ 
 
    answer = firstInput * secondInput; 
 
    $("#topBar").text(answer); 
 
}); 
 

 
$("#resetButton").click(function(){ 
 
    input = ""; 
 
    firstInput = ""; 
 
    secondInput = ""; 
 
    answer =""; 
 
    $("#topBar").empty(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> 
 
<div id="container"> 
 
    <div id="topBar"></div> 
 
    <div class="row"> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button7">7</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button8">8</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button9">9</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle operation" id="buttonDivide">&divide;</button> 
 
    </div> 
 
    </div> 
 
    <div class="row"> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button4">4</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button5">5</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button6">6</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle operation" id="buttonx">x</button> 
 
    </div> 
 
    </div> 
 
    <div class="row"> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button1">1</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button2">2</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button3">3</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle operation" id="buttonMinus">-</button> 
 
    </div> 
 
    </div> 
 
    <div class="row"> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="button0">0</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle numbers" id="buttonDot">.</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle" id="buttonEqual">=</button> 
 
    </div> 
 
    <div class="butStyle"> 
 
     <button class="numStyle operation" id="buttonAdd">+</button> 
 
    </div> 
 
    </div> 
 
    <button id ="resetButton">Reset</button> 
 
</div>

+0

全く動作しません。あなたは誰かにあなたのための電卓のアプリを書くことを望みますか? –

+0

'答え'、 'firstInput'、' secondInput'は '''の代わりに' null'に設定する方がよいでしょう。 – ValLeNain

+0

私が使っているJSFiddleはここにあります。 https://jsfiddle.net/kFiggins/nqwoz01h/。乗算のみが動作します。 – Kyler

答えて

1

問題は、演算子を押すたび、新しいクリックリスナを登録しているという事実から来ています: ))、既に呼び出されている場合は、このコードを呼び出さないようにする必要があります。

+0

はい、私はこれが私にとっては初心者にとっては良いプロジェクトだと思っていましたが、私のスキルレベルではかなり難しいと判明しました。あなたは間違いなく正しいです。すべての演算子でそのコードを呼び出さない方法を見つける必要があります。助けてくれてありがとう!! – Kyler

1

空()関数は、選択した要素の子ノードを削除し、私はあなただけの何か他のものでコンテンツを交換するかもしれないと思います(空の文字列や0のように)か、代わりにhtml()関数を使用してください。あなたは方法は(私は多くのことをリファクタリングしたいロジックを維持したい場合は

$(".numbers").click(function(){ 
    $("#topBar").text(secondInput += $(this).text()) 
}); 

+0

ありがとう、私はそれを試みたが、それは動作しませんでした。私がリセットを押した後は、計算機はクリアされますが、私が積み重ねて2番目の入力を選択すると、選択したものの2倍が選択されます。私は入力がどのように扱われているかに問題があると思います。ご協力いただきありがとうございます! – Kyler

+0

ええ、私はjsfiddleと@ValLeNainの答えをテストしていたが、正しいとマークする必要があります。 – LordNeo