Skip to content

Instantly share code, notes, and snippets.

@gorborukov
Created February 2, 2015 23:10
Show Gist options
  • Save gorborukov/9065d153297061d66e1b to your computer and use it in GitHub Desktop.
Save gorborukov/9065d153297061d66e1b to your computer and use it in GitHub Desktop.
options
<div class="optionals">
<% @sections.each_with_index do |section, index| %>
<div class="field">
<label><b><%= section.name %></b></label>
<%= hidden_field_tag "hotel[option_ids][]", nil %>
<% section.options.each do |option| %>
<label><%= check_box_tag "hotel[option_ids][]" %>
<%= option.name %></label>
<% end %>
</div>
<% end %>
<%= f.hidden_field :optional %>
</div>
<script type="text/javascript">
$(document).ready(function(){
checkbox = $('.optionals input[type=checkbox]');
var getList = $("#hotel_optional").val().split(",");
$('.optionals input[type=checkbox]').each(function () {
console.log(getList);
var i;
for (i=0;i<getList.length;++i) {
if (getList[i] == 1) {
$(checkbox[i]).prop('checked', true);
}
if (getList[i] == 0) {
$(checkbox[i]).prop('checked', false);
}
}
});
$(".optionals input[type='checkbox']").on('change', function(){
var setList = "";
$('.optionals input[type=checkbox]').each(function () {
var setThisVal = (this.checked ? "1" : "0");
setList += (setList=="" ? setThisVal : "," + setThisVal);
});
$("#hotel_optional").val(setList);
console.log(setList);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment