2016-12-29 4 views
2

私のjQueryである:NaN値を取得するのはなぜですか?

$("#listDB").on("change", ".qfStyle", function(event) { 
    var id = $(this).parents("tr").find('.itemCbox').val(); 
    $qnty = $("#qnty"+id); 
    $unit = $("#unit"+id); 
    $price = $("#price"+id); 

    if($(this).parents("tr").find('.pbo').text()=="Kilos") 
    { 
     if(this.value<1) 
     { 
      $qnty.text(this.value*1000); 
      $unit.text("gm"); 

      var data = "action=getPrice&id="+id; 
      $.post("addBill.php", data, function(json) { 
       alert(json.price); 
       $price.text(json.price*this.value); 
      }, 'json'); 
     } 
    } 
}); 

サーバによって返されたJSONデータがある:

{ "価格": "52.00"}ここ

thisは、テキストボックスを指し。私は、発現のための値NaNを取得しています:

$price.text(json.price*this.value); 

しかし、私はthis.valuejson.priceの両方が数字であることを確実にしました。だから、なぜ私はそれらを掛けるときにNaNを得るのですか?

+0

(1)私が使用することを考える: 'this.value'が原因スコープにアクセスできなくなっているのでjson.price.value(2)$に.getメソッド –

+3

にしてみてくださいません。これの値を別の変数に格納し、その変数を使用して価格を取得します。 – magreenberg

+0

this.valueを変数に割り当ててください。たとえば、$ price = $( "#price" + id); $ value = this.value; $ price.text(json.price * this.value)行を変更します。 $ price.text(json.price * $ value)に;それが機能しない場合は、この変数を乗算する前に10進数に変換してください。 –

答えて

8

thispostの機能の範囲外です。

以下のコードを確認してください。私はthis.valueの値を保持し、postの機能でも利用できるはずの新しい変数valueを追加しました。

$("#listDB").on("change", ".qfStyle", function(event) { 
    var id = $(this).parents("tr").find('.itemCbox').val(); 
    $qnty = $("#qnty"+id); 
    $unit = $("#unit"+id); 
    $price = $("#price"+id); 

    var value = this.value; 

    if($(this).parents("tr").find('.pbo').text()=="Kilos") 
    { 
     if(value<1) 
     { 
      $qnty.text(value*1000); 
      $unit.text("gm"); 

      var data = "action=getPrice&id="+id; 
      $.post("addBill.php", data, function(json) { 
       alert(json.price); 
       $price.text(json.price*value); 
      }, 'json'); 
     } 
    } 
}); 
0

Number()を使用すると、文字列番号を数値形式に抽出できます。

function getStringNumber(stringNumber){ 
 
    var formatNumber = Number(stringNumber); 
 
    console.log(formatNumber); 
 
}
<html> 
 
<button onclick="getStringNumber('24.00')"> Convert String Number </button> 
 
</html>

関連する問題