我有一个包含许多类别的请求表格,类别有许多产品。我让 jQuery 根据 CATEGORY 选择填充 PRDOUCTS:
requests.js.coffee:
jQuery ->
$('#request_product_id').parent().hide()
products = $('#request_product_id').html()
console.log(products)
$('#request_category_id').change ->
category = $('#request_category_id :selected').text()
options = $(products).filter("optgroup[label='#{category}']").html()
console.log(options)
if options
$('#request_product_id').html(options)
$('#request_product_id').parent().show()
else
$('#request_product_id').empty()
$('#request_product_id').parent().hide()
现在我想在选择产品时显示 product.description 的静态文本。
同时,我想根据所选产品显示自定义表单部分显示。例如,如果 product.id = 1,则渲染 _product1.html.erb 或.... 只需根据 product.id 切换 DIV 以显示适当的字段。
这是我的完整 _form.html.erb:
<%= form_for(@request) do |f| %>
<% if @request.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@request.errors.count, "error") %> prohibited this request from being saved:</h2>
<ul>
<% @request.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :category_id, "Select a Category:" %>
<%= f.collection_select(:category_id, Category.sorted, :id, :name, :include_blank => true ) %>
</div>
<!-- BASED ON CATEGORY SELECTED ABOVE, LOAD CORRESPONDING PRODUCTS BELOW -->
<div class="field">
<%= f.label :product_id, "Select a Product/Service:" %>
<%= f.grouped_collection_select :product_id, Category.sorted, :products, :name, :id, :name, include_blank: true %>
</div>
<!-- ALL OF THE ABOVE WORKS FINE... NOW.... -->
<!-- BASED ON PRODUCT SELECTED ABOVE, LOAD CORRESPONDING PRODUCT DESCRIPTION AND CORRESPONDING FORM -->
<div class="field">
:::: THIS WILL AUTO POPULATE WITH THE ABOVE-SELECTED PRODUCT.DESCRIPTION ::::
</div>
<p></p>
<div>
<p>:::: BASED ON PRODUCT.ID, HIDE/SHOW SPECIFIC DIVS HERE. IF PRODUCT.ID IS NOT SPECIAL, DO NOT SHOW ANYTHING HERE</p>
</div>
<!-- THEN, NO MATTER WHAT THE PRODUCT.ID IS, THERE WILL BE A REQUESTOR_NOTE FOR FURTHER INFO -->
<div class="field">
<%= f.label :requestor_note, "Please give full details below..." %>
<%= f.text_area :requestor_note, :size => "50x6" %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>