false},class:"bt-6ren">
gpt4 book ai didi

javascript - 根据 Ruby on Rails 中的另一个下拉菜单更改下拉菜单

转载 作者:行者123 更新时间:2023-12-03 01:35:27 25 4
gpt4 key购买 nike

目前我有一个如下所示的表单:

<tr>
<td>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_record"
</td>
<td><%= f.date_field :date, as: :date, value: f.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %> </td>
<td><%= f.text_field :description, label: false, class: 'form-control input' %></td>
<td><%= f.text_field :reference, label: false, class: 'form-control input' %></td>
<td> <%= f.collection_select :bank_account_id, BankAccount.all, :id, :name, {:prompt => false},class:"btn btn-sm" %></td>
<td><%= f.collection_select :gl_account_id, GlAccount.all, :id, :name, {:prompt => false},class:"btn btn-sm" %></td>
<td><%= f.collection_select :vat_type, Transaction.vat_types.map{ |dp| [dp.first, dp.first.humanize] }, :first, :second,{:prompt => false},class:"btn btn-sm" %></td>
<td> <%= f.text_field :total_amount, class: 'form-control input' %></td>
<% f.check_box :payment, :value => true %>

</table>

我想添加另一个<td>在我删除提示用户选择付款类型的列之后:

<select> 
<option value="regular">Regular</option>
<option value="invoice">Invoice</option>
</select>

这会改变行:

<td><%= f.collection_select :gl_account_id, GlAccount.all, :id, :name, {:prompt => false},class:"btn btn-sm" %></td>

将会变成:

<td><%= f.collection_select :purchase_id, Purchase.all, :id, :invoice_number, {:prompt => false},class:"btn btn-sm" %></td>

这样,用户就可以轻松更改他们正在执行的事务类型,而无需重定向到新表单。关于如何实现这一目标有什么想法吗?

最佳答案

1 => 删除链接后添加交易类型选择选项

<%=select_tag "transaction_type", options_for_select([['regular', 'Regular'], ['invoice', 'Invoice']]), class: 'transaction_type'%>

2 => 在 select_field 上添加一个唯一 ID,该 ID 将在交易类型更改时动态触发

<td><%= f.collection_select :gl_account_id, GlAccount.all, :id, :name, {:prompt => false},class:"btn btn-sm", id: 'gl_account' %></td> 
<td><%= f.collection_select :vat_type, Transaction.vat_types.map{ |dp| [dp.first, dp.first.humanize] }, :first, :second,{:prompt => false},class:"btn btn-sm", id: "vat_type" %></td>

3 => 使用 Jquery

<script>
//by default hide both gl_account and vat_type select box
$('#vat_type, #gl_account').hide();
// on change transaction_type show/hide these select box
$('.transaction_type').on('change', function(){
var transaction_type_Val = $(this).val();
if (transaction_type_Val == 'regular'){
$('#gl_account').show();
$('#vat_type').hide();
}else{
$('#gl_account').hide();
$('#vat_type').show();
}
})
</script>

关于javascript - 根据 Ruby on Rails 中的另一个下拉菜单更改下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51093901/

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