簡體   English   中英

jQuery增量結帳號碼

[英]jquery increment checkout number

我有一個span字段,該字段具有一個通過對會話數組中的項目進行計數而生成的數字。 當用戶將商品添加到購物車時,我需要將此值增加1。我嘗試了parseInt函數,但它似乎不起作用。 控制台顯示正在選擇該ID。 只是似乎無法增加1。

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;

任何幫助都會很棒

在您的示例中,您實際上並沒有增加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);

您需要使用parseInt函數在jquery中添加值

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

試試這個,這很好。

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

工作演示

 $(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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM