[英]Display text based on a selected option in Rails app
The option to select the country is through 选择国家的选项是通过
<%= f.select :country_id, options_for_select(Country.all.order(:name).map{ |c| [t(c.code2.to_sym, :scope => :countries), c.id] }, selected: @customer.try(:country).try(:id)), wrapper_html: {:class=>'form-control', id:"country_name"} %>
And I have a text field to enter the country telephone code through this 我有一个文本字段可通过此字段输入国家/地区电话代码
<%= f.text_field :phone_country, :class=>"form-control", wrapper_html: {id: "telephone_code"} %>
I want the telephone code to be entered automatically based on the country selected in the previous option. 我希望根据上一个选项中选择的国家/地区自动输入电话代码。 I have the country codes in the database and I also understand I have to use jQuery function, but couldn't get further.
我在数据库中有国家/地区代码,而且我也明白我必须使用jQuery函数,但无法继续使用。 Thanks.
谢谢。
$(".country_id").on('change', function(){
$('.phone_country').val($(this).val());
});
The above function will be called when we change value of dropdown. 当我们更改下拉列表的值时,将调用上述函数。
<%= f.select :country_id, options_for_select(Country.all.order(:name).map{ |c| [t(c.code2.to_sym, :scope => :countries), c.id] }, selected: @customer.try(:country).try(:id)), wrapper_html: {:class=>'form-control country_id', id:"country_name"} %>
<%= f.text_field :phone_country, :class=>"form-control phone_country", wrapper_html: {id: "telephone_code"} %>
Update: I tried to do a Ajax request with the following code. 更新:我尝试使用以下代码进行Ajax请求。 But still couldn't figure out how to insert data based on the drop-down.
但是仍然无法根据下拉列表确定如何插入数据。
$('.country-id').on 'change', (e) ->
$.ajax
type: 'GET'
url: '/new'
data: 'country-id'
success: (data) ->
alert data
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.