简体   繁体   中英

How to output responses based on checkbox values stored in an array?

I have persistent checkboxes which represent the days of the week. I was able to store these values in localStorage in an array so that they stay after refresh.

What I want to know is how can I output "Weekday" or "Weekend" based on the selection of the days? For example a user selects "Monday" and "Wednesday" and the ouput is "Weekday"

I've tested it out using cases but was unsuccessful with applying that with the values from localStorage . Is there something wrong with how I've stored my values in the array?

Here is my codepen .

My function for storing and keeping the values checked:

function updateStorage() {
    $checkboxes.each(function() {
        formValues[this.id] = this.checked;
    });

    localStorage.setItem("formValues", JSON.stringify(formValues));
}

$checkboxes.on("change", function() {
    updateStorage();
});

// On page load
$.each(formValues, function(key, value) {
    $("#" + key).prop('checked', value);
});

Here's the issue. Once written to Localstorage, you need to remove the item in order to re-write it

//checkbox

//function for persistent checkbox     
var formValues = JSON.parse(localStorage.getItem('formValues')) || {};
var $checkboxes = $("#checkbox-wrapper :checkbox");

function updateStorage(){
  $checkboxes.each(function() {
    formValues[this.id] = this.checked;
  });
  localStorage.removeItem('formValues');
  localStorage.setItem("formValues", JSON.stringify(formValues));
}

$checkboxes.on("change", function(){
  updateStorage();
});

// On page load

$.each(formValues, function(key, value) {
  $("#output").html($("#output").html() + key + ": " + value + "<br/>")
});

I updated your code in codepen with above and it works.

Here is a function that will check your days object and return an object with 2 boolean properties ( isWeekDay and isWeekendDay ):

function checkweekday(days) {
  console.log('days', days);
  var weekDays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
  var weekendDays = ['saturday', 'sunday'];
  var isWeekDay = false;
  var isWeekendDay = false;
  for (var day in days) {
    if (days[day]) {
      if (day) {
        if (weekDays.indexOf(day) > -1) {
          isWeekDay = true;
        }
        if (weekendDays.indexOf(day) > -1) {
          isWeekendDay = true;
        }
      }
    }
  }
  return {
    isWeekDay: isWeekDay,
    isWeekendDay: isWeekendDay
  }
}

After retrieving values from localStorage, call the function with the object.

var formValues = JSON.parse(localStorage.getItem('formValues')) || {};
var check = checkweekday(formValues);
console.log(check);

Here is an updated plunkr .

I hope you mean something like this

//checkbox

//function for persistent checkbox     
var formValues = JSON.parse(localStorage.getItem('formValues')) || {};
var $checkboxes = $("#checkbox-wrapper :checkbox");

function outputResult(isWeekday,isWeekend){

  var output = ""
  if( isWeekday && isWeekend ) output = "Both";
  else if( isWeekday ) output = "Weekday";
  else if( isWeekend ) output = "Weekend";
  document.getElementById("output").innerHTML = output;

}

function checkDay(){

    var isWeekday = false;
    var isWeekend = false;

    $checkboxes.each(function(){
      if( this.checked ){
        if( this.id == "saturday" || this.id == "sunday" ) isWeekend  = true;
        else isWeekday = true;
      }
    });

  outputResult(isWeekday,isWeekend);
}

function updateStorage(){

  $checkboxes.each(function(){
    formValues[this.id] = this.checked;
  });

  localStorage.setItem("formValues", JSON.stringify(formValues));

  checkDay();
}

$checkboxes.on("change", function(){
  updateStorage();
});

// On page load
$.each(formValues, function(key, value) {
  $("#" + key).prop('checked', value);
});

https://codepen.io/anon/pen/gepomY?editors=1011

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