简体   繁体   中英

Update Table Element without Reading/Writing to A Database

Currently I have a table which looks like the following: 在此输入图像描述

Now what I want to do is that, when someone clicks the checkmark button it switches to some other icon (could be anything) But, I want to have it so that when the page refreshes, the icon is not the green check mark but the new icon now.

Is there a way in which this is possible without reading/writing to a database?

You can use cookies, so that you save this information as a dictionary in the browser:

With this function you can save cookies:

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

Nota: exdays parameter defines when the cookie expires in days.

And with this you can get the value of the cookie even if you refresh the browser:

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

If you need more information you can find it here: https://www.w3schools.com/js/js_cookies.asp

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