为什么在 html 值 = false 时选中复选框

why checkboxes are check while html value = false

当我进入编辑页面时,我的所有复选框都被选中,而当我查看 html 时,我将其中一些设置为 false :

<input checked="false" class="hidden" id="cover_inspiration_image_ids" name="cover[inspiration_image_ids][]" type="checkbox" value="68">

我不明白为什么 checked 值不起作用。 我也试过 checked="checked"

我试过 jquery : $(".hidden").prop('checked') 结果 return true

这是我的代码:

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

能否尝试将值传递为 Boolean 而不是 String

checked="#{check_status(img, @cover)}"
#to
checked=check_status(img, @cover)

完整代码

INPUT[type="checkbox" name="cover[inspiration_image_ids][]" id='cover_inspiration_image_ids' value="#{img.id}" class="hidden" checked=check_status(img, @cover)]

如果您不想选中复选框,则根本不呈现其具有任何值的 checked 属性。

相关:What values for checked and selected are false?