saving checkbox state on reload

后端 未结 3 1781
孤独总比滥情好
孤独总比滥情好 2020-12-06 13:12

how do i save checkbox state through sessions? i\'m using this jquery code to toggle the options:

$(\'div.check input:checkbox\').bind(\'change\',function(){         


        
相关标签:
3条回答
  • 2020-12-06 13:47

    Purely in JavaScript supporting localStorage if available, otherwise using document.cookie.

    function getStorage(key_prefix) {
        // this function will return us an object with a "set" and "get" method
        // using either localStorage if available, or defaulting to document.cookie
        if (window.localStorage) {
          // use localStorage:
          return {
            set: function(id, data) {
              localStorage.setItem(key_prefix+id, data);
            },
            get: function(id) {
              return localStorage.getItem(key_prefix+id);
            }
          };
        } else {
          // use document.cookie:
          return {
             set: function(id, data) {
               document.cookie = key_prefix+id+'='+encodeURIComponent(data);
             },
             get: function(id, data) {
               var cookies = document.cookie, parsed = {};
               cookies.replace(/([^=]+)=([^;]*);?\s*/g, function(whole, key, value) {
                  parsed[key] = decodeURIComponent(value);
               });
               return parsed[key_prefix+id];
             }
           };
         }
      }
    
    jQuery(function($) {
      // a key prefix is used for the cookie/storage
      var storedData = getStorage('com_mysite_checkboxes_'); 
    
      $('div.check input:checkbox').bind('change',function(){
        $('#'+this.id+'txt').toggle($(this).is(':checked'));
        // save the data on change
        storedData.set(this.id, $(this).is(':checked')?'checked':'not');
      }).each(function() {
        // on load, set the value to what we read from storage:
        var val = storedData.get(this.id);
        if (val == 'checked') $(this).attr('checked', 'checked');
        if (val == 'not') $(this).removeAttr('checked');
        if (val) $(this).trigger('change');
      });
    
    });
    

    jsFiddle demo available -- Click some checkboxes, then "Run" the script again!

    0 讨论(0)
  • 2020-12-06 13:47

    If it's not something that you need to save in the database I recommend using cookies instead of sessions. You can use the easy jQuery cookies plugin.

    0 讨论(0)
  • 2020-12-06 13:55

    There is a way, but you need to make some change- off course it will work if you submit through form or JQuery with same parameter name (here name_txt and ref_txt).

    give checkbox input a name

    <?php
    if(isset($_REQUEST['name_txt']))
        $name_text = $_REQUEST['name_txt'];
    if(isset($_REQUEST['ref_txt']))
        $ref_text = $_REQUEST['ref_txt'];
        ?>
        <p><input type="checkbox" name = "name_txt" value="Name" id="name" <?php echo ($name_text == 'Name' || !isset($name_text))?'checked':'';?> /> 
        <label for="name">Name</label></p>  
        <p><input type="checkbox" name = "ref_txt" value="Reference " id="reference" <?php echo ($ref_text == 'Reference' || !isset($ref_text))?'checked':'';?> /> <label for="reference">Reference</label></p>
    
    0 讨论(0)
提交回复
热议问题