<% form_for :credit_card, @cc, :url => '/credit_cards', :method => :post do |f| %>
<%= f.hidden_field "job_id", :value => params[:job_type_id] %>
<%= f.hidden_field "job_type", :value => "Job Hourly" %>
<%= f.hidden_field "lead_id", :value => @job.lead_id %>
<center><strong>ADD New Credit Card</strong> </center>
<br/>
<table align="left">
<tr>
<td> <label style="padding-left: 30px;">Type</label></td>
<td><%= f.select(:card_type, CreditCard::TYPES, {:prompt => "Select Type", :selected => ""}) %></td>
</tr>
<tr>
<td> <label style="padding-left: 30px;">Number</label></td>
<td><%= f.text_field "card_number", "size" => 16, 'maxlength' => 16%></td>
</tr>
<tr>
<td> <label style="padding-left: 30px;">Expiry</label></td>
<td> <select id="cc_state" name="credit_card[expiration_month]">
<%= options_for_select( [[ "Select Month", "" ]] + CreditCard::MONTHS )%>
</select>
<select id="cc_state" name="credit_card[expiration_year]">
<%= options_for_select( [[ "Select Year", "" ]] + ((Date.today.year - 5)..(Date.today.year + 12)).collect{|x| [x,x]}.reverse )%>
</select>
</td>
</td>
</tr>
<tr>
<td> <label style="padding-left: 30px;">CVV</label></td>
<td><%= f.text_field "security_code", "size" => 4, 'maxlength' => 4%></td>
</tr>
<tr>
<td> <label style="padding-left: 30px;">Expiry</label></td>
<td> <select id="cc_state" name="credit_card[expiration_year]">
<%= options_for_select( [[ "Select Year", "" ]] + ((Date.today.year - 5)..(Date.today.year + 12)).collect{|x| [x,x]}.reverse )%>
</select>
</td>
</tr>
<tr>
<td> <label style="padding-left: 30px;">Location</label></td>
<td><%= f.select(:id, @job_extra_stops.collect{|s| ["#{s.city},#{s.zip},#{s.state}", s.id]},:onchange => "display_dropdown(this)",:id => "dropdown_id", :prompt => "Select Location")%> </td>
</tr>
<tr><td> <label style="padding-left: 30px;">Street</label></td>
<td><%= f.text_field "billing_street_address", "size" => 20 %></td></tr>
<tr><td> <label style="padding-left: 30px;">City</label></td>
<td><%= f.text_field "billing_city", "size" => 20 %></td></tr>
<tr><td> <label style="padding-left: 30px;">State</label></td>
<td><select id="cc_state" name="credit_card[billing_state]">
<%= options_for_select( [[ "Select State", "" ]] + State::NAMES )%>
</select></tr>
<tr><td> <label style="padding-left: 30px;">Zip</label></td>
<td><%= f.text_field :billing_zip, "size" => 12 %></td></tr>
<tr>
<td colspan="2" align="center"><%= submit_tag 'Save' %></td>
</tr>
这是视图文件中的代码。目前,信用卡模型属性在表单中手动输入,然后保存。但是,我不想手动输入,而是要导入job_extra_stop模型中已存在的那些属性值。当从下拉列表中选择该选项时,我想将下拉列表中的值(来自job_extra_stop模型)映射到信用卡模型属性
这意味着当我选择特定选项时,州,邮政编码,城市的值应在文本框中显示,并在提交后保存在credit_card模型中。
我正在使用rails 1.2.2和ruby 1.8.6
谢谢
答案 0 :(得分:2)
在脚本中编写以下代码
function display() {
var val1 = ""
var val2 = ""
var e = ""
e = document.getElementById('display_id')
val1 = e.options[e.selectedIndex].textContent
val2 = val1.split(',')
document.getElementById('street').value = val2[0]
document.getElementById('city').value = val2[1]
document.getElementById('state').value = val2[2]
document.getElementById('zip').value = val2[3]
}
在代码
后添加select标签<%= select_tag :id1, options_for_select([[ "Select Location", "" ]] + @job_extra_stops.collect{|s| ["#{s.st_name},#{s.city},#{s.state},#{s.zip}", s.st_name]}),:onchange => "display()", :id => 'display_id', :include_blank => 'Please add locations above' %>