Storing checkbox value in local storage

前端 未结 3 1141
梦毁少年i
梦毁少年i 2021-01-07 00:28

I have a checkbox whose value $row[\'uid\'] I would like to store in local storage using javascript or jquery. When the user \"unchecks\" the checkbox, the valu

3条回答
  •  不思量自难忘°
    2021-01-07 00:56

    Here's an example to get you headed in the right direction:

    var boxes, arr;
    
    // This function loads the array and then checks the uid of each checkbox
    // against it.
    
    function checkBoxes() {
      arr = loadBoxArray();
      $.each($('input[type=checkbox]'), function (i, el) {
        if (arr.indexOf(i) > -1) {
          $(this).prop('checked', true);
        } else {
          $(this).prop('checked', false);
        }
      });
    }
    
    // If the localStorage item is available, load it into an array
    // otherwise set a default array and save it to localStorage
    
    function loadBoxArray() {
      if (localStorage.getItem('boxes')) {
        arr = loadBoxArray();
      } else {
        arr = [0, 2];
        saveBoxArray(arr);
      }
    }
    
    // Save the array to localStorage    
    
    function saveBoxArray(arr) {
      localStorage.setItem('boxes', JSON.stringify(arr));
    }
    
    // On page load check the boxes found in localStorage
    
    checkBoxes();
    
    // Clicking a checkbox will update the checkbox array which is then saved to localStorage
    
    $('input[type=checkbox]').click(function () {
      var arr = $.map($('input[type=checkbox]:checked'), function (el) {
        return $(el).data('uid');
      });
      saveBoxArray(arr);
    });
    

提交回复
热议问题