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" or "Personnel" - すべてのソフトウェアまたは人事製品を正常にロードします。ただし、category.name = "Application Development" または "Business Objects Report" の場合、何も読み込まれません。
どうして?前もって感謝します!