[英]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);
$(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.