vendredi 22 février 2019

Save multiple checkbox[] check state on page refresh using local storage in php

I have problem to show user checkbox check state after page is refreshed. I tried using local storage but it checks all the checkboxes please help! I have use ajax to load different pages to select the row by checkbox This is input value

<input type="checkbox" id="checkselect" data-name="checkselect[]" class="get_value" value="<?php echo $row['car_booking_id'];?>" onchange="CheckedChange(this)">

And this is my javascript

<script>
 function CheckedChange(obj){  
           var id =  []; 
           id=obj.value;       // id.push($(obj).val());               
           id = id.toString();
           //alert(id);  
           if($(obj).is(":checked"))  
                { 
                     $.ajax({  
                          url:"close_booking_check_state.php",  
                          method:"POST",  
                          data:{id:id},  
                          success:function(data){  
                               $('#result').html(data);  
                          }  
                     }); 
                } 

          else {
                       $.ajax({  
                          url:"close_booking_check_state_delete.php",  
                          method:"POST",  
                          data:{id:id},  
                          success:function(data){  
                               $('#result2').html(data);  
                          }  
                     });          
               }
     }
</script>
<script>

  $(function() {
    // Clicking on an <input:checkbox> should check any children checkboxes automatically
    $('input:checkbox').change( fnTest );
    display();
  });

  function fnTest(e) {
     var checked = [];
  $('input:checkbox:checked').each(function() {
      localStorage.setItem("prevChecked",check);
      var test1=checked.push( $(this).data('name'));
      alert("t",test1);
  });
  //alert("You have selected:\n\n - " + checked.join("\n - ") );
 // localStorage.setItem("prevChecked",checked.join('|'));
  }

  function display(){
  var test=localStorage.getItem("prevChecked");
  alert("r",test);
  //var results = test.split('|');
 // for (var r=0, len=results.length; r<len; r++ ) {
    $('input[data-name="'+test+'"]').prop('checked', true);
  //}
}

</script>




Aucun commentaire:

Enregistrer un commentaire