根据Ruby on Rails中的combobox中选择的值动态更改表单 [英] Dynamically change form based on value selected in combobox in Ruby on Rails
问题描述
我有以下表格:
Services
ID| Service_type_id| desc| date|payment_terms
Service_types
ID|NAME|isCredit
Payment_Terms
ID|Payment_Term
服务表包含服务列表。服务类型定义了提供的服务类型。一些service_types是信用卡提供的 - isCredit字段包含布尔值1或0。问题是当用户添加服务时 - 我有一个填充了服务类型的组合框。但是,我需要的是,当用户选择isCredit值为1的服务类型时,它应该在同一表单上显示一个组合框,其中包含付款条款表中的付款条款选项。
The services table contains a list of services. The service type defines the kind of service offered. Some service_types are offered on credit - the isCredit field holds a boolean 1 or 0 value. The thing is that when a user adds a service - I have a combo box populated with the service types. However what I need is that when the user chooses a service type whose isCredit value is 1, it should show on the same form a combo box with options of payment terms from the payment terms table.
如何在ruby on rails上完成?我只希望当选择的service_type具有isCredit值1时显示payment_term组合框,否则services.payment_terms = 0
How can this be done in ruby on rails? I only want that the payment_term combo box be displayed when the service_type chosen has an isCredit value 1 else the services.payment_terms = 0
推荐答案
首先,将带有付款条件的选择框渲染为隐藏(例如,使用特定的css类)。我们假设它有id payment_terms
。
First, render the select box with payment terms as hidden (by using specific css class for example). Let's assume it has id payment_terms
.
然后渲染服务选择框带有附加数据属性以跟踪服务 isCredit
0 | 1
Then render services select box with additional data attribute to keep track if service isCredit
0|1
<select id=services>
<% services.each do |service| %>
<option value=<%=service.id %> data-iscredit=<%=service.type.isCredit ? '1' : '0' %>><%=service.desc %></option>
<% end %>
</select>
在JS中编写一个简单的脚本来处理更改
选择填充服务的事件和显示/隐藏 payment_terms
在 isCredit
上选择依赖。我假设你正在使用jQuery。
Write a simple script in JS to handle change
event on the select populated with services and show/hide payment_terms
select depening on isCredit
. I'm assuming you're using jQuery.
$("#services").change(function() {
var selopt = $(this.children[this.selectedIndex]),
isCredit = parseInt(selopt.attr('data-iscredit'));
if (isCredit === 0) {
$("#payment_terms").hide();
} else {
$("#payment_terms").show();
}
})
这篇关于根据Ruby on Rails中的combobox中选择的值动态更改表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!