繁体   English   中英

如何在Rails中显示html options_from_collection_for_select

[英]How to show html in rails options_from_collection_for_select

我使用rails options_from_collection_for_select进行选择。

options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id))

def tenant_text_value
  "<a href='http://www.google.com'>google</a>".html_safe
end

我需要<option value="215"><a href='http://www.google.com'>google</option> ,当我单击a我转到Google。

但是,我只是得到<option value="215">Google</option>

如果我使用"<a href='http://www.google.com'>google</a>".to_s"<a href='http://www.google.com'>google</a>".to_s得到<option value="215"><a href='http://www.google.com'>google</a></option> 只是文本,而不是链接。

我该怎么办 ?

我想我知道您想做什么,而您想做什么却有缺陷。

我认为您要做的是当某人选择要转到页面的内容时

预期产量

<select id="abc">
    <option value="http://www.google.com">Google</option>
    <option value="http://www.yahoo.com">Yahoo</option>
    <option value="http://www.google.com">Bing</option>
</select>

$("#abc").change(function() {
    window.location.href = $(this).val();
});

滑轨

<%= 
  select_tag "abc", 
  options_from_collection_for_select(@tenants, @tenant.url, @tenant.name", @tenant.id) 
%>

我希望这可以帮助您

快乐黑客

<%= select_tag '<some_id>', options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id)),:onchange => "window.location.href(this.value);" %>

要么

<%= select_tag '<some_id>', options_from_collection_for_select(@tenants, :id, :tenant_text_value, current_tenant.try(:id)) %>

<script>
    $(function(){
      $('#team_id').bind('change', function () {
         var url = $(this).val()
          if (url) {
              window.location.href(url);
          }
          return false;
      });
    });
</script>

暂无
暂无

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

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