禁用复选框并使用Cookie

时间:2018-05-04 17:23:52

标签: javascript cookies checkbox

这将保持选中复选框:

function setCookie(c_name, value, expiredays) {
  var exdate = new Date()
  exdate.setDate(exdate.getDate() + expiredays)
  document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate)
}

function getCookie(c_name) {
  if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=")
    if (c_start != -1) {
      c_start = c_start + c_name.length + 1
      c_end = document.cookie.indexOf(";", c_start)
      if (c_end == -1) c_end = document.cookie.length
      return unescape(document.cookie.substring(c_start, c_end))
    }
  }
  return null
}
onload = function() {
  document.getElementById('myCheck').checked = getCookie('myCheck') == 1 ? true : false;
}

function set_check() {
  setCookie('myCheck', document.getElementById('myCheck').checked ? 1 : 0, 100);
}
<input type="checkbox" value="1" id="myCheck" onclick="set_check();"> 

我还想在选中后禁用该复选框,并使用Cookie将其禁用。如何将该功能添加到现有代码中?谢谢!

1 个答案:

答案 0 :(得分:0)

EnhancedMvxListView