Javascript 基于Rails应用程序中的选定选项显示文本
选择国家/地区的选项是通过Javascript 基于Rails应用程序中的选定选项显示文本,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,选择国家/地区的选项是通过 <%= 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_n
<%= 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"} %>
:countries),c.id]},选中:@customer.try(:country.try(:id)),包装器\u html:{:class=>'form-control',id:“country\u name”}%>
我有一个文本字段,可以通过这个字段输入国家电话号码
<%= f.text_field :phone_country, :class=>"form-control", wrapper_html: {id: "telephone_code"} %>
“表单控件”,包装器\u html:{id:“电话\u代码”}%>
我希望根据在上一选项中选择的国家/地区自动输入电话代码。我在数据库中有国家代码,我也知道我必须使用jQuery函数,但无法进一步了解。谢谢
$(".country_id").on('change', function(){
$('.phone_country').val($(this).val());
});
当我们更改下拉列表的值时,将调用上述函数
<%= 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"} %>
当我们更改下拉列表的值时,将调用上述函数
<%= 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"} %>
谢谢但是
这个
插入国家ID,而我想插入国家ID
的相应电话代码。您必须执行ajax请求,并根据该请求设置电话国家文本字段值对不起,我执行了ajax请求,但仍然无法更改电话国家。你能详细说明一下吗?很抱歉在你的回答中包含了更新。我以为我是在更新我的帖子,但却错误地编辑了你的。希望你不介意,我知道了。我将data
属性添加到options
tagofcountries列表中,并通过$(this.find(“:selected”).data(“\u dataname”)
)从jQuery访问该数据。谢谢。但是这个
插入国家ID,而我想插入国家ID
的相应电话代码。您必须执行ajax请求,并根据该请求设置电话国家文本字段值对不起,我执行了ajax请求,但仍然无法更改电话国家。你能详细说明一下吗?很抱歉在你的回答中包含了更新。我以为我是在更新我的帖子,但却错误地编辑了你的。希望你不介意,我知道了。我将data
属性添加到options
tagofcountries列表中,并通过$(this.find(“:selected”).data(“\u dataname”)
从jQuery访问该数据。