简体   繁体   中英

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

I have this bit of code which carries some numeric values within the span but when u click on checkbox it subtract 1 from the main value, basically it subtracts 1 from the total value, how to achieve this even if the form is submitted the deducted value in span remains same unless the checkbox is unchecked?

HTML is here with 10 for example value:

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

Your time and help would be appreciated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM