NaNを返し続ける関数があります。私はフォーラムを検索し、見つけたものをすべて試しましたが、明らかに私の(おそらく)奇妙なコードを見て何か間違ったことをしています:)
私のコード:
function bakVormTotals(targetClass){
$.getJSON('http://shop.com/cart/?format=json', function(data){
var totaal = 0;
$('.grandTotal').each(function(){ ////////////
var item = $(this); // FOUND THIS ON FORUM
totaal+= parseInt(item.val()); ///////////
})
$.each(data.cart.totals, function(index, totals){
$('<strong/>').html('€' + (+totals.grand_total).toFixed(2)).appendTo(targetClass); // the grand total price
});
});
}
jsonは次のようになり"totals":{"sub_total":"335.29","taxes":[{"percentage":"0.1900","amount":63.71}],"grand_total":"399.00"}}
ます。それが役立つ場合。
どんな助けでも大歓迎です
更新1
わかりました、私はいくつかの新しい面白いものを見つけました。上記の関数を次のように変更しました。
function bakVormTotals(targetClass){
$.getJSON('http://shop.com/cart/?format=json', function(data){
$.each(data.cart, function(index, totals){
$('<strong/>').html('€' + totals.grand_total + '').appendTo(targetClass); // the grand total price
});
});
}
これで、スクリプトは次のようになります。€undefined€undefined€undefined€undefined€undefined€undefined€undefined€undefined€1197.00
€1197.00が正しい値なので、明らかに私は正しい方向に向かっています。「未定義」のものはおそらくhtml部分が原因ですか?$('')部分を「total」のようなものに変更してHTML部分を省略できるようにするために、一部の本文を使用できますか?