2015-03-31 82 views
0

我有一個跨度字段,其中包含通過計算會話數組中的項目生成的數字。當用戶添加一個項目到購物車時,我需要將此值增加1.我嘗試過parseInt函數,但它似乎不起作用。控制檯顯示正在選擇的ID。好像不能用1jquery增值結算編號

jQuery代碼遞增是:

var checkout_number = parseInt($('#checkout-count').html()); 
console.log(checkout_number); // Shows value 2 etc 

經電流checkout_number成功遞增1:

$.parseInt($("#checkout-count").val(),10) + 1; 

任何幫助將是巨大的

+0

在jQuery中,有沒有像$ .parseInt – 2015-03-31 10:21:22

回答

0

在你的例子中,你實際上並沒有增加checkout-count元素的HTML值 - 試試這個:

// use the HTML of the checkout-count span as the current checkout number 
var checkout_number = parseInt($('#checkout-count').html(), 10); 
// replace the HTML of the checkout-count span with the old value plus one 
$('#checkout-count').html(checkout_number + 1); 
+0

這解決了這個問題 - 將其標記爲答案時,它讓我:) – jamper 2015-03-31 10:22:57

+0

使用的.text ()而不是html()以獲得更優化的解決方案! – FarazShuja 2015-03-31 10:23:17

0

您需要使用parseInt功能jQuery中

var checkout_number = parseInt($('#checkout-count').text(), 10); 
$('#checkout-count').text(checkout_number + 1); 
0

增加值試試這個能正常工作。

$('#checkout-count').text(parseInt($("#checkout-count").text()) + 1); 

Working Demo

0

$(function() { 
 
    $('#checkout-count').text(function() { 
 
    return +(this.innerHTML) + 1; 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<span id="checkout-count">2</span>