@job.l-6ren">
gpt4 book ai didi

javascript - 如何使用 jquery/ajax 将下拉列表的选定值显示到文本框中

转载 作者:行者123 更新时间:2023-11-30 18:04:24 24 4
gpt4 key购买 nike

 <% 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>

这是 View 文件中的代码。目前信用卡模型属性是在表单中手动输入然后保存。但是我不想手动输入,而是想导入 job_extra_stop 模型中已经存在的那些属性值。当从下拉列表中选择该选项时,我想将下拉列表的那些值(来自 job_extra_stop 模型)映射到信用卡模型属性

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

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

谢谢

最佳答案

在脚本中编写如下代码

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' %>

关于javascript - 如何使用 jquery/ajax 将下拉列表的选定值显示到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16162487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com