jeudi 28 mai 2020

Keep check box checked after refreshing the page

I have checkboxes as below in my rails app

<% if field["field_type"] == "checklist"%>
 <% templat = @custom_templates.select{|s| s["id"] == m["startup_field_template_id"]}.first%>
 <small>
   <% vals = field["options"].to_s.gsub(/[\[\"\]]/, '').split(',') %>
   <% vals.each do |v| %>
   <% if templat["option_multi"]%>
     <input type="checkbox" />&nbsp;&nbsp;&nbsp; <%= v %><br/>
   <% else %>
     <input type="checkbox" class="check" />&nbsp;&nbsp;&nbsp; <%= v %><br/>
   <% end %>
   <% end %>
 </small>
<% else %>
 n/a
<% end %>

I used below code to keep checked checkbox 'checked' after refreshing the page

<sript>
$(function(){
var test = localStorage.input === 'true'? true: false;
$('input').prop('checked', test || false);
});

$('input').on('change', function() {
localStorage.input = $(this).is(':checked');
console.log($(this).is(':checked'));
});
</script>

But it keep all check boxes checked after refreshing the page. how can I solve this?




Aucun commentaire:

Enregistrer un commentaire