REQUEST 表单有很多 CATEGORIES,CATEGORY 有很多 PRODUCTS。选择类别以填充产品选择列表。这是jQuery:
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()
这是部分表格:
<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>
如果类别名称中没有空格,则此方法非常有用,但如果有,则不会加载产品列表。例如: category.name = "Software" 或 "Personnel" - 加载所有软件或人事产品就好了。但如果 category.name = "Application Development" 或 "Business Objects Report",则不会加载任何内容。
怎么会?提前致谢!