Rails 助手“集合选择”

发布于 2024-09-08 04:49:34 字数 976 浏览 2 评论 0原文

我和 这个人 遇到了完全相同的问题,因为没有人回答,我决定重新发布:

我一直在尝试实现这个 jquery 插件到我的应用程序。我需要帮助 尝试输出类似的内容

<select name="user[university_id]" id="user_university_id" class="selectable">
<option value="1" title="uni1">Uni1</option>
<option value="2" title="uni2">Uni2</option>
</select>

通过使用 Rails 助手...问题 助手似乎从来没有输出过 选项标签的标题属性.. 这对于这个插件至关重要

请帮忙,提前致谢

编辑:我当前的 Rails 代码是

<%= f.collection_select(:university_id,University.all,:id,:name)%

它只是输出

<选择 name="user[university_id]" id="user_university_id">
        <选项值=“1”>Uni1
        <选项值=“2”>Uni2
    

所以基本上我需要的是一种方法 还将标题属性添加到我的 选项。

I have the exact same problem as this guy , since no one has answered, I decided to repost:

I have been trying to implement this
jquery plugin to my app.I need help
trying to output something like this

<select name="user[university_id]" id="user_university_id" class="selectable">
<option value="1" title="uni1">Uni1</option>
<option value="2" title="uni2">Uni2</option>
</select>

by using a rails helper...the problem
is the helpers never seem to output a
title attribute to the option tags..
which is critical for this plugin

please help, thanks in advance

Edit: my current rails code is

<%= f.collection_select(:university_id,University.all,:id,:name)%

which simply outputs

<select name="user[university_id]" id="user_university_id">
        <option value="1">Uni1</option>
        <option value="2">Uni2</option>
    </select>

So basically what I need is a way to
also add title attribute to my
options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-09-15 04:49:42

它有点hacky,但你可以这样做:

$('#user_university_id option').each( function(){
    var optText = $(this).html();
    $(this).attr('title', optText);
})

这将用选项的文本内容分配标题。或者,您可以使用生成的 ID 执行更任意的操作。

Its a bit hacky but you could do something like:

$('#user_university_id option').each( function(){
    var optText = $(this).html();
    $(this).attr('title', optText);
})

This would assign the title with the text contents of the option. Alternatively you could do something more arbitrary using a generated ID.

自由范儿 2024-09-15 04:49:39

你有没有想过推出自己的助手?我刚刚从 rails 源中复制了这段代码 然后更改名称并添加标题。

  def options_for_select_with_title(container, selected = nil)
    return container if String === container

    container = container.to_a if Hash === container
    selected, disabled = extract_selected_and_disabled(selected)

    options_for_select = container.inject([]) do |options, element|
      text, value = option_text_and_value(element)
      selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
      disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
      options << %(<option title="#{html_escape(value.to_s.downcase)}" value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}>#{html_escape(text.to_s)}</option>)
    end

    options_for_select.join("\n").html_safe
  end

Have you thought about rolling your own helper? I just copied this code out of the rails source and then changed the name and added the title.

  def options_for_select_with_title(container, selected = nil)
    return container if String === container

    container = container.to_a if Hash === container
    selected, disabled = extract_selected_and_disabled(selected)

    options_for_select = container.inject([]) do |options, element|
      text, value = option_text_and_value(element)
      selected_attribute = ' selected="selected"' if option_value_selected?(value, selected)
      disabled_attribute = ' disabled="disabled"' if disabled && option_value_selected?(value, disabled)
      options << %(<option title="#{html_escape(value.to_s.downcase)}" value="#{html_escape(value.to_s)}"#{selected_attribute}#{disabled_attribute}>#{html_escape(text.to_s)}</option>)
    end

    options_for_select.join("\n").html_safe
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文