繁体   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