Html 如何使用 check_box_tag 在 Haml 中选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12957348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:27:29  来源:igfitidea点击:

How to check a check box in Haml using check_box_tag

htmlrubycheckboxhaml

提问by user1756646

Can someone tell me how to set these check boxes to checked? I'm sure it's simple, but after an hour of trying i think I need to ask! Thanks!

有人可以告诉我如何将这些复选框设置为选中状态吗?我确定这很简单,但经过一个小时的尝试,我想我需要问一下!谢谢!

= form_tag movies_path, :id => 'ratings_form', :method => :get do
  Include: 
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]", 
  = submit_tag 'Refresh', :id => 'ratings_submit'

回答by Salil

Ref check_box_tag

参考check_box_tag

check_box_tag "ratings[#{rating}]",  1, !!(rating.rating)

Your 2nd parametermust be valueof checkbox

2nd parameter必须是value复选框

Your 3rd parametermust be a boolean conditionwhich return true/falseand depends on it checkbox is checked/unchecked

3rd parameter必须是一个boolean condition返回true/false并取决于它复选框是checked/unchecked

回答by Matias Yaryura

check_box_tag "ratings[#{rating}]",  1, @selected.include?("#{rating}")

where @selectedis an array with the element selected.

其中@selected是选择元素的数组。

回答by rghjfjh

Use true for Checked or false for Unchecked at the end of the line

在行尾对 Checked 使用 true 或对 Unchecked 使用 false

check_box_tag "ratings[#{rating}]", true #checked

or

或者

check_box_tag "ratings[#{rating}]", false #unchecked

回答by FletchRichman

According to the api dock, check box tag takes the following options:

根据api Dock,复选框标签采用以下选项:

check_box_tag(name, value = "1", checked = false, options = {})

check_box_tag(name, value = "1", checked = false, options = {})

This means the first value is the name, the second value is a 'value' and the third value is whether the box is checked, which is default to false. So, in order to check or uncheck the box you can do the following:

这意味着第一个值是名称,第二个值是“值”,第三个值是是否选中该框,默认为 false。因此,为了选中或取消选中该框,您可以执行以下操作:

- if (some condition)
  = check_box_tag "ratings[#{rating}]", "anystring", true
- else 
  = check_box_tag "ratings[#{rating}]" 

The second line just puts a random string into the value field because in this case it does not matter.

第二行只是将一个随机字符串放入 value 字段,因为在这种情况下它无关紧要。

回答by Ryan Snodgrass

Building on the answer by Sali. Strangely, the check_box_tagreturns a checkbox with no label text. Here's how you can display text if you're iterating over an array.

基于 Sali 的回答。奇怪的是,check_box_tag返回一个没有标签文本的复选框。如果您在数组上进行迭代,您可以通过以下方式显示文本。

- Puppies.each do |puppy|
  = check_box_tag(puppy.name, puppy.name, puppy.goodboy?)
  = puppy.name

回答by Sunny Sharma

= check_box_tag "ratings[#{rating}]",{},{:checked => ""}