我有一段代码,它在范围内携带一些数值,但是当您单击复选框时,它会从主值中减去 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>
关于javascript - 如何将跨度减去的值保存到 cookie 直到复选框未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512134/