Ruby on Rails 标记函数。为什么radio_button_tag.id的大小写与label.for不匹配?

发布于 2024-12-06 20:17:25 字数 2560 浏览 0 评论 0原文

在注册 openstreetmap 时,在条款页面上,我注意到单击标签并没有检查与其关联的单选按钮。这是 html:

<!-- legale is GB --> 
<form action="/user/terms" method="post"> 
  <p> 
    Please select your country of residence:

    <input id="legale_fr" name="legale"
onchange="Element.update(&quot;contributorTerms&quot;, &quot;&lt;img alt=\&quot;Searching\&quot; src=\&quot;/images/searching.gif?1313181320\&quot; /&gt;&quot;);; new Ajax.Request('/user/terms?legale=FR', {asynchronous:true, evalScripts:true})" 
                                            type="radio" value="FR" /> 
    <label for="legale_FR">France</label>

    <input id="legale_it" name="legale" ... type="radio" value="IT" /> 
    <label for="legale_IT">Italy</label> 

    <input checked="checked"
           id="legale_gb" name="legale" ... type="radio" value="GB" /> 
    <label for="legale_GB">Rest of the world</label> 
  </p> 
</form> 

如您所见,复选框 id="legale_gb" 与标签 for="legale_GB" 不匹配。

现在 openstreetmap 的网站实际上是开源的,因此我们可以阅读 terms.html.erb

<!-- legale is <%= @legale %> -->
<% form_tag :action => 'terms' do %>
  <p>
    <%= t 'user.terms.legale_select' %>
    <% [['france', 'FR'], ['italy', 'IT'], ['rest_of_world', 'GB']].each do |name,legale| %>
      <%=
        radio_button_tag 'legale', legale, @legale == legale,
          :onchange => remote_function(
            :before => update_page do |page|
              page.replace_html 'contributorTerms', image_tag('searching.gif')
            end,
            :url => {:legale => legale}
          )
      %>
      <%= label_tag "legale_#{legale}", t('user.terms.legale_names.' + name) %>
    <% end %>
  </p>
<% end %>

我是 Rails 新手,但我看不到任何小写单选按钮标记的 id 的内容。更重要的是,即使我查看 radio_button_tag, sanitize_to_id 我看不到在哪里这是来自。

有人知道是什么原因造成的吗?

编辑根据答案将我的描述中的label换成radio

While registering for openstreetmap, on the terms page, I noticed that clicking the labels didn't check the radio buttons associated with them. Here is the html:

<!-- legale is GB --> 
<form action="/user/terms" method="post"> 
  <p> 
    Please select your country of residence:

    <input id="legale_fr" name="legale"
onchange="Element.update("contributorTerms", "<img alt=\"Searching\" src=\"/images/searching.gif?1313181320\" />");; new Ajax.Request('/user/terms?legale=FR', {asynchronous:true, evalScripts:true})" 
                                            type="radio" value="FR" /> 
    <label for="legale_FR">France</label>

    <input id="legale_it" name="legale" ... type="radio" value="IT" /> 
    <label for="legale_IT">Italy</label> 

    <input checked="checked"
           id="legale_gb" name="legale" ... type="radio" value="GB" /> 
    <label for="legale_GB">Rest of the world</label> 
  </p> 
</form> 

As you can see the checkbox id="legale_gb" doesn't match the label for="legale_GB".

Now openstreetmap's website is actually open source so we can read the terms.html.erb:

<!-- legale is <%= @legale %> -->
<% form_tag :action => 'terms' do %>
  <p>
    <%= t 'user.terms.legale_select' %>
    <% [['france', 'FR'], ['italy', 'IT'], ['rest_of_world', 'GB']].each do |name,legale| %>
      <%=
        radio_button_tag 'legale', legale, @legale == legale,
          :onchange => remote_function(
            :before => update_page do |page|
              page.replace_html 'contributorTerms', image_tag('searching.gif')
            end,
            :url => {:legale => legale}
          )
      %>
      <%= label_tag "legale_#{legale}", t('user.terms.legale_names.' + name) %>
    <% end %>
  </p>
<% end %>

I'm a rails newbie, but I can't see anything there that lowercases the id of the radio button tag. What's more, even when I look at the source of radio_button_tag, sanitize_to_id I can't see where this is coming from.

Anyone got any idea what's causing this?

Edit Swapped out label for radio in my description according to answer from

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

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

发布评论

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

评论(1

树深时见影 2024-12-13 20:17:25

2 things:

  1. Wrong tag, the offender is radio_button_tag (it's capped as expected in the label).

  2. Seems like you're linking to the wrong Rails. According to this project's environment.rb, it's using Rails 2.3.14. If you look at radio_button_tag for that release, you'll see the culprit.

    # our pretty tag value is downcased on line 318
    pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase
    
    # although the pretty name is not, oddly
    pretty_name = name.to_s.gsub(/\[/, "_").gsub(/\]/, "")
    
    # then the two are combined into the HTML id
    html_options = { ..., "id" => "#{pretty_name}_#{pretty_tag_value}", ... }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文