简体   繁体   English

Ruby on Rails:在html.erb中保留复选框状态

[英]Ruby on Rails: Preserve Checkbox States in html.erb

How can I preserve the states of checkbox using session? 如何使用会话保留复选框的状态?

I tried the following but does not work. 我尝试了以下操作,但不起作用。

   <form name="size">
  <input type="checkbox" name="size[]" value="1" <%@selected.include?("1")%> /> 1
  <input type="checkbox" name="size[]" value="2" <%@selected.include?("2")%> /> 2
  <input type="checkbox" name="size[]" value="3" <%@selected.include?("3")%> /> 3
  <input type="submit" value="Filter" /><br></form>

@selected is a instance variable in controller that stores the states of the checkbox using session. @selected是控制器中的一个实例变量,它使用会话存储复选框的状态。

Assuming @selected is an array, as I can infer from your code example, you can do 假设@selected是一个数组,我可以从您的代码示例中推断出,您可以

<input type="checkbox" name="size[]" value="1" <%= @selected.include?("1") ? "checked" : "" %> /> 1
<input type="checkbox" name="size[]" value="2" <%= @selected.include?("2") ? "checked" : "" %> /> 2
<input type="checkbox" name="size[]" value="3" <%= @selected.include?("3") ? "checked" : "" %> /> 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM