Javascript 如何根据选中的选项显示文本数组?

Javascript 如何根据选中的选项显示文本数组?,javascript,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我正在尝试根据选中的复选框显示文本 我有3个状态,所以当我选中一个或两个复选框时,将显示未使用javascript选择的选项 例如: if I check1 will show me the text active. if I check2 will show me the text inactive. if I check3 will show me the text inactive and active. if I check1 and check 2 will show me the t

我正在尝试根据选中的复选框显示文本

我有3个状态,所以当我选中一个或两个复选框时,将显示未使用javascript选择的选项

例如:

if I check1 will show me the text active.
if I check2 will show me the text inactive.
if I check3 will show me the text inactive and active.
if I check1 and check 2 will show me the text "active","inactive".
if I check1 and check 2 will show me the text "active","removed".
if I check1 and check 2 will show me the text "removed","inactive".
只需根据选中的复选框保留文本

这是控制器:

@search_state = params[:search_state]
@people = Person.all
以下是视图:

<%= form_tag(people_path,:method => "get") do %>
 #### BEGIN CHECKBOX #####
 <% if @search_state.blank? %> 
   Check1 <%= check_box_tag "search_state[]", "0" %>
   Check2 <%= check_box_tag "search_state[]", "1" %>
   Check3 <%= check_box_tag "search_state[]", "2" %>
 <% else %>
   Check1 <%= check_box_tag "search_state[]", "0", @search_state.collect(&:to_i).include?(0) %>
   Check2 <%= check_box_tag "search_state[]", "1", @search_state.collect(&:to_i).include?(1) %>
   Check3 <%= check_box_tag "search_state[]", "2", @search_state.collect(&:to_i).include?(2) %>
 <% end %>
  #### END CHECKBOX ####

 ### WANT TO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ###
   <% if @search_state.to_s=="0"%>
     Active
   <% elsif @search_state.to_s=="1"%>
     Inactive
   <% elsif @search_state.to_s=="2" %>
     Removed
   <% elsif @search_state.to_s==["0","1"] %>
     Active,Inactive
   <% elsif @search_state.to_s==["0","2"] %>
     Active,Removed
   <% elsif @search_state.to_s==["1","2"] %>
     Inactive,Removed
   <% elsif @search_state.to_s==["0","1","2"] %>
     Active,Inactive,Removed
   <% end %>
   ### END WANTTO SHOW A TEXT ACCORDING CHECKBOXES SELECTED ### 
 <% end %>
我的代码太长,我认为这不是正确的方法

例如,在这种情况下,我有3个状态,如果我有更多的状态将太长和混乱

有人能帮我用RubyonRails做一个没有几行代码的复选框吗

我知道使用Javascript是解决方案,但这是解决问题的唯一方法,请问解决方案是什么


我将非常感谢您的帮助。

我很难从您的示例中理解您的意图。但我想你的意思是按钮1是“活动的”,按钮2是“非活动的”,按钮3是“移除的”。此外,我认为您希望没有任何选项是相互排斥的……特别是,您可以同时选中“活动”和“非活动”。您还暗示,将来可能会有更多类似的复选框

如果所有这些都是正确的,那么我同意,您当前的方法将无法很好地扩展

我不会试图根据复杂的标准选择一条完整的消息,而是对其进行重构,从其组件构建消息。以下几点应该能让你达到目的:

  flags = []
  flags << 'Active' if @search_state.collect(&:to_i).include?(0)
  flags << 'Inactive' if @search_state.collect(&:to_i).include?(1)
  flags << 'Removed' if @search_state.collect(&:to_i).include?(2)

  puts flags.join(', ')
flags=[]

你能解释一下你想要达到的目标吗?我不认为你做“它”的方式是正确的,但我不确定“它”是什么。。。你能写出函数性的预期行为吗?对不起,Yoshiji先生,我的意思是没有根据复选框写几行代码,如果我有更多的复选框,代码就会太长,我认为这不是使它更具活力的正确方法。天哪,你是主人,我注意到你没有太多的分数来证明你知道。谢谢大卫,这对我很有用。谢谢你的时间和经验。
  flags = []
  flags << 'Active' if @search_state.collect(&:to_i).include?(0)
  flags << 'Inactive' if @search_state.collect(&:to_i).include?(1)
  flags << 'Removed' if @search_state.collect(&:to_i).include?(2)

  puts flags.join(', ')
   Check1 <%= check_box_tag "search_active", "1", @search_active == 1) %> Active
   Check2 <%= check_box_tag "search_inactive", "1", @search_inactive == 1) %> Inactive
   Check3 <%= check_box_tag "search_removed", "1", @search_removed == 1) %> Removed