【问题标题】:Setting default value of Dropdown in HTML - Special Case在 HTML 中设置下拉菜单的默认值 - 特殊情况
【发布时间】:2013-03-14 05:27:43
【问题描述】:

我的 Rails 视图 (index.html.erb) 中有多个下拉框。代码如下:

......
......
<td>
<% @builds.each_with_index do |row,index|
    if row2[0].to_s == row_s[0] %> 
      (When this condition is true I want to set the value of the dropdown list)
          (The value could be PASS, FAIL or PENDING which comes from the database as row_s[7])

       <form id=<%= "build_status_form#{index}" %>>
           <select name="condition" 
             id=<%= "build_status#{index}" %>  onchange="this.form.submit()">
                   <option value="PENDING">PENDING</option>
                   <option value="PASS">PASS</option>
                   <option value="FAIL">FAIL</option>
           </select>
       </form>

    <% end %>
<% end %>
</td>

如何动态设置每个下拉菜单的默认值?

【问题讨论】:

  • 这里有什么问题

标签: javascript ruby-on-rails html erb


【解决方案1】:

这可以使用 rails 助手在一行中完成

<form id=<%= "build_status_form#{index}" %>>
  <%= select_tag :condition, options_for_select(%w[PENDING PASS FAIL], row_s[7]), id: "build_status#{index}", onchange: 'this.form.submit()' %>
</form>

【讨论】:

  • 这太棒了。您能解释一下如何提交表单 onchange 事件吗?
  • 我也试过了,但似乎有一些语法错误:语法错误,意外':',期待')' ...ASS FAIL],row3 [7]),onchange:' this.form.submit()');@output...
  • 尝试将onchange:更改为:onchange =&gt;
  • 啊,感谢您的编辑,但这是在 ruby​​ 1.9 中编码哈希的另一种方式
【解决方案2】:

假设如果你想设置默认值PASS使用selected

  <form id=<%= "build_status_form#{index}" %>>
       <select name="condition" 
         id=<%= "build_status#{index}" %>  onchange="this.form.submit()">
               <option value="PENDING">PENDING</option>
               <option value="PASS" selected="selected">PASS</option
               <option value="FAIL">FAIL</option>
       </select>
   </form>

【讨论】:

    【解决方案3】:
    <form id=<%= "build_status_form#{index}" %>>
               <select name="condition" 
                 id=<%= "build_status#{index}" %>  onchange="this.form.submit()">
    
                       if(your value is true)
                       {
                           if value is pending then 
                             <option selected="selected" value="PENDING">PENDING</option>
                           else
                            <option  value="PENDING">PENDING</option>
    
                           like this you can write
                      } 
               </select>
           </form>
    

    我在这里像 javascript 一样写。你可以在 ralis 中根据需要写。

    【讨论】:

    • 您可以检查我放置 cmets 的条件。我不知道在 ralis 中如何编写条件
    猜你喜欢
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2019-09-25
    相关资源
    最近更新 更多