在 rails 上使用 ruby 中的选择框更新文本字段
Update textfield using a selectbox in ruby on rails
我是 rails 的新手,下面有一个表格,我需要使用 select 框中的 selected 选项更新文本字段(汽车价格)(调整价格)。下拉菜单需要有多个百分比选项,如 100% 、 50% 和 25% 。当一个百分比被 selected 时,它应该用新计算的调整价格更新该字段中的汽车价格。
<% car = Car.find(params[:id]) %>
<table class="part" width="100%">
<tr style="vertical-align: top;">
<td><b>Car Model</b></td>
<td><b>Price</b></td>
<td><b>Adjusted price</b></td>
</tr>
<tr style="vertical-align: top;">
<td><%= text_field(‘Car’, ‘car_name’, size: 10, value: check_for_car_name(car.name)) %></td>
<td><%= text_field(‘Car’, ‘price’, size: 10, value: check_for_car_price(car.price)) %></td>
<td><%= select(adjusted price(not sure of this part)) %></td>
</tr>
</table>
我不确定 select 如何即时更新汽车价格的框?提前致谢。
看看rails guides,你可以结合使用options_for_select
和select_tag
<%= select_tag(:city_id, options_for_select([["25%", 1], ["50%", 2]])) %>
这将输出
<option value="1">25%</option>
<option value="2">50%</option>
现在,要动态更新价格,您可能需要在前端使用 javascript,例如将 event listener 添加到 select。然后,当 selected 选项为例如 50% 时,您可以通过更改它的值直接在价格字段上进行计算。
document.getElementById('price').value = calculated price
或者由于问题被标记为 jQuery,您可以使用 .val()
$("#price").val() = calculated price
我是 rails 的新手,下面有一个表格,我需要使用 select 框中的 selected 选项更新文本字段(汽车价格)(调整价格)。下拉菜单需要有多个百分比选项,如 100% 、 50% 和 25% 。当一个百分比被 selected 时,它应该用新计算的调整价格更新该字段中的汽车价格。
<% car = Car.find(params[:id]) %>
<table class="part" width="100%">
<tr style="vertical-align: top;">
<td><b>Car Model</b></td>
<td><b>Price</b></td>
<td><b>Adjusted price</b></td>
</tr>
<tr style="vertical-align: top;">
<td><%= text_field(‘Car’, ‘car_name’, size: 10, value: check_for_car_name(car.name)) %></td>
<td><%= text_field(‘Car’, ‘price’, size: 10, value: check_for_car_price(car.price)) %></td>
<td><%= select(adjusted price(not sure of this part)) %></td>
</tr>
</table>
我不确定 select 如何即时更新汽车价格的框?提前致谢。
看看rails guides,你可以结合使用options_for_select
和select_tag
<%= select_tag(:city_id, options_for_select([["25%", 1], ["50%", 2]])) %>
这将输出
<option value="1">25%</option>
<option value="2">50%</option>
现在,要动态更新价格,您可能需要在前端使用 javascript,例如将 event listener 添加到 select。然后,当 selected 选项为例如 50% 时,您可以通过更改它的值直接在价格字段上进行计算。
document.getElementById('price').value = calculated price
或者由于问题被标记为 jQuery,您可以使用 .val()
$("#price").val() = calculated price