lundi 17 avril 2017

jquery check if checkboxes is checked

I'm trying to check the value of my checkboxes. I tried .is(':checked')and .prop('checked')

At first load, it properly detect check and uncheck boxes. But then it acts as all checkboxes are set to true... Same thing happen with both method. This is my code :

$(document).on('ready page:load', function () {
    $(".hidden").each(function(){
        if ($(this).prop('checked')){
            console.log($(this))
            $(this).next().next().css("display", "block");
        }
        $(this).click(function(){
            $(this).next().next().toggle("display")
        })
    })
})

covers/_form

 - @inspiration_images.each do |img|
   - next if img.image.path.nil?
     .w-col.w-col-2.w-col-small-4.w-col-tiny-6
       label
         INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden" checked="#{check_status(img, @cover)}"]
          .image-cov-pick
            = cl_image_tag(img.image.path, height: 190, class: 'img-to-pick').html_safe
           .white

cover_helper

module CoversHelper
  def check_status(image, cover)
    cover.inspiration_image_ids && cover.inspiration_image_ids.include?(image.id)
  end
end

Aucun commentaire:

Enregistrer un commentaire