簡體   English   中英

如何保存從跨度到Cookie的減值,直到取消選中復選框

[英]How to save the subtracted value from span to cookie till checkbox unchecked

我有這段代碼在跨度內包含一些數值,但是當您單擊復選框時,它從主值中減去1,基本上從總值中減去1,即使提交表單扣除的值也如何實現跨度保持不變,除非未選中該復選框?

HTML此處有10個示例值:

<input type="checkbox" id="featured" name="featured" <?php echo $checked;?> /> (<span id="credit">10</span>)

JS:

<script>
$(function () {
  $('#featured').change(function () {
    var currentValue = parseInt($('#credit').text());
    var newValue = currentValue + ($(this).prop('checked') ? -1 : 1);
    $('#credit').text(newValue);
  });
});
</script>

您的時間和幫助將不勝感激。

<script>
$(function () {
  $('#featured').change(function () {
    var savedValue = localStorage.checkboxValue;
    var currentValue = savedValue ? parseInt(savedValue) : parseInt($('#credit').text());
    var newValue = currentValue + ($(this).prop('checked') ? -1 : 1);
    $('#credit').text(newValue);
    localStorage.checkboxValue = newValue;
  });
});
</script>

暫無
暫無

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

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