2016-05-26 3 views
1

でラベルテキストを更新し、私は私のWebページ上のラベルを持って、は、HTMLのラベルテキストを取得して数値に変換し、インクリメントして、その結果

私は1つによって数(最初は1)の増分を取得したい
<label id="testLabelq" class="no_border" style="position: relative; top:-50px; margin: 0 auto; padding: 0px; width:20%; height: 25px;">1</label> 

このボタンを押すと、ラベルの内容を新しい値に設定します。私はラインに変更した場合

<button id="testButtonplus" class="item_button" type="Button" style="position: relative; left:-40%; margin: 0 auto; padding: 0px; width:20%; height: 50px;" onclick="add()">+</button> 

私のJavaScript関数は

...私もボタンを押すと、ラベルの値が変更されません。この

 function add() 
    { 
      var quantity_temp = document.getElementById("testLabelq").innerText; 

      var qantity_int = parseInt(quantity_temp, 10) + 1; 

      document.getElementById("testLabelq").innerHTML = "Test " + quantity_int.toString(); 
    } 

のように見えます

document.getElementById("testLabelq").innerHTML = "Test " + quantity_int.toString(); 

~

document.getElementById("testLabelq").innerHTML = "Test " + quantity_temp; 

すべてのボタンを押すたびにラベルテキストが更新され、「テスト」が追加されます。

したがって、私の数値変換は間違っていなければならないと思うが、私はどこを見ることができない。

おかげで、 グレアムあなたが関数内の構文エラーを持っている

答えて

1

あなたのコードは少し単純化したときに動作するようです

1つの修正がtypo qantity_intである必要がありますquantity_int

+0

ハハありがとう、私は誤植を修正し、それは働いた。 – SlyRaccoon

2

(VAR qantity_int =):

function add() { 
 
    var quantity_temp = document.getElementById("testLabelq").innerText; 
 
    var quantity_int = parseInt(quantity_temp, 10) + 1; 
 
    document.getElementById("testLabelq").innerHTML = quantity_int.toString(); 
 
}
<label id="testLabelq">1</label> 
 
<button id="testButtonplus" class="item_button" type="Button" onclick="add()">+</button>

function add() { 
    var quantity_temp = document.getElementById("testLabelq").innerText; 
    var quantity_int = parseInt(quantity_temp, 10) + 1; 
    document.getElementById("testLabelq").innerHTML = quantity_int.toString(); 
} 
関連する問題