How do I make my checkbox keep its checked status when the search is reloaded

1

My problem is that I have checkboxes in a table and when I give it checked, and I want to search in my search bar, at the moment it shows me the result of the search, and I return to my original table, the status is deleted " Checked. " I tried with sessionstorage but still it does not work at all well.

while ($fila = $resultado->fetch_assoc()) {
        $idchk+=1;
?>          
        <tr>  

            <td>
                <input name='checkimp[]' id='<?php echo $idchk ?>' type='checkbox' value='<?php echo $fila['id'] ?>' 
                onclick="if(this.checked)sessionStorage.setItem(this.id,this.value);else sessionStorage.removeItem(this.id);">
            </td>

            <td><?php echo $fila['id'] ?></td>
            <td><?php echo $fila['nombre'] ?></td>
            <td><?php echo $fila['telef1'] ?></td>
            <td><?php echo $fila['telef2'] ?></td>
            <td><?php echo $fila['telef3'] ?></td>
            <td><?php echo $fila['distrito'] ?></td>
            <td><?php echo $fila['direccion'] ?></td>
            <td><?php echo $fila['nrocasa'] ?></td>
            <td><?php echo $fila['nrodpto'] ?></td>
            <td><?php echo $fila['referencia'] ?></td>
            <td><?php echo $fila['urb'] ?></td>                


            <td><input type="text" name="cosas" id='cosas'></td>

            <td>

            <a class='btn btn-danger' href='#'onclick='preguntar("<?php echo $fila['id'] ?>")'>
                <i class='icon-trash'></i>
            </a>
            <a class='btn btn-info' href='editar.php?edit="<?php echo $fila['id'] ?>"'>
                <i class='icon-edit'></i>
            </a>


            <button type='submit' class='btn btn-default'>
                        <i class='icon-print'></i>
                    </button> 


            </td>

        </tr><script type="text/javascript">
        document.getElementById('log').innerHTML='Valores checkeados:<br>'; 
    for (var i=0; i<sessionStorage.length; i++){ 
        var key = sessionStorage.key(i); 
        document.getElementById(key).checked=1; 
        document.getElementById('log').innerHTML+=sessionStorage.getItem(key); 
    } 
    if(!i)document.getElementById('log').innerHTML+='ninguno'; </script>

I must remember that my search is with ajax, and the page is not recharged, but if it could be maintained when recharging it would also be great, but I do not know how I can do it, thank you very much in advance.

$(buscar_datos());

function buscar_datos(consulta){
	$.ajax({
		url: 'buscar.php ' ,
		type: 'POST' ,
		dataType: 'html',
		data: {consulta: consulta},
	})
	.done(function(respuesta){
		$('#datos').html(respuesta);
	})
	.fail(function(){
		console.log("error");
	});
}


$(document).on('keyup','#caja_busqueda', function(){
	var valor = $(this).val();
	if (valor != "") {
		buscar_datos(valor);
	}else{
		buscar_datos();
	}
});
    
asked by Luis Fernando 13.08.2018 в 13:37
source

0 answers