REQUEST表格有很多类别,CATEGORY有很多产品。选择“类别”以填充“产品选择”列表。这是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”,则不会加载任何内容。
为什么?提前谢谢!
答案 0 :(得分:0)
请在jQuery中尝试options = $(products).filter("optgroup[label='#{category}']").html()
。
答案 1 :(得分:0)
我想通了 - 我在#category周围添加了单引号:
options = $(products).filter("optgroup[label='#{category}']").html()
感谢您让我说出来! :)