当特定语言与红宝石一起被选择时,如何使用JavaScript所选择的功能? [英] How to use JavaScript selected functionality when particular language is selected in conjunction with ruby?

查看:60
本文介绍了当特定语言与红宝石一起被选择时,如何使用JavaScript所选择的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尝试做什么

我有一个html选择框.选择框负责选择首选语言.例如,如果某个页面显示在德国,则选择框应显示德国为首选语言,其他语言也是如此.

I have a html select box. The select box is responsible for selecting the preferred languages. for instance if a page is displayed in Germany then the select box should display Germany as its preference and likewise for the other languages.

完成了哪些事情

在application.html.erb中,我有以下代码

In application.html.erb I have below code

#application.html.erb

<select name="language" onChange="location = this.options[this.selectedIndex].value;">
    <% @langs.each_pair do |short, long| %>
    <option value="/<%= short %>/<%= @rem %>" <% if short==@langu %> selected="selected"<% end %>><%= long %></option>
    <% end %>
</select>

在application_controller.rb中,我有以下代码

In application_controller.rb I have below code

#application_controller.rb
@string = request.fullpath
@langu  = @string.split("/")
@rem    = @string.split("/#{@langu[1]}/")
@langu  = @langu[1]
@rem    = @rem[1]

@langs = { :en => 'english', 
           :es => 'espaniol', 
           :de => 'germany', 
           :fr => 'french', 
           :it => 'italy' }

什么不起作用

当用户以英语/西班牙语/德语 OR 查看网站时,选择框显然应显示特定语言.即如果是英语,然后是英语,如果是西班牙语,则是西班牙文,依此类推.这确实是行不通的

When user views a website in English/Spanish/Germany OR selects from the list, the select box evidently should show the particular language. i.e. if English then english, if Spanish then espaniol and so on. This is the exact thing not working

正在做什么

如果用户从选择框中选择了首选语言(比如说西班牙语),则会呈现特定页面,但是选择框显示的是英语,其本质上应该为espaniol.

If the user selects the preferred language (lets say Spanish) from the select box it renders the specific page, however the select box displays english which in essence should be espaniol.

谢谢!

推荐答案

您的字符串中只有一个小的错字.它应该是selected='selected',所以它应该像这样工作:

There's just a minor typo in your string. It should be selected='selected', so it should work like this:

<select name="language" onChange="location = this.options[this.selectedIndex].value;">
<% @langs.each_pair do |short, long| %>
<option value="/<%= short %>/<%= @rem %>" <% if @short==@langu %> selected="selected"<% end %>><%= long %></option>
<% end %>
</select>

这篇关于当特定语言与红宝石一起被选择时,如何使用JavaScript所选择的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆