1
 <% 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 模型)映射到信用卡模型属性

这意味着当我选择特定选项时,state、zip、city 的值应显示在文本框中的下方,并在提交后保存在 credit_card 模型中。

我正在使用 rails 1.2.2 和 ruby​​ 1.8.6

谢谢

4

1 回答 1

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_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' %>
于 2013-04-23T14:11:16.313 回答