我正在使用cocoon gem创建带有动态选择菜单的嵌套表单,事情是我无法传递表单ID,因为它是动态生成的。
实施例
id="document_document_lines_attributes_1498139518118_porhseq"
id="document_document_lines_attributes_1498139518118_itemno"
形式:
<div class="col-md-2">
<div class="form-group">
<%= f.collection_select(:porhseq, PurchaseOrderHeader.all, :id, :ponumber,
{ include_blank: "Select PO" }, {class: "form-control"} ) %>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<%= f.grouped_collection_select(:itemno, PurchaseOrderHeader.order(:ponumber),
:purchase_order_details, :ponumber, :id, :itemno,
{ include_blank: "Select Item" }, { class: 'form-control' }) %>
</div>
</div>
咖啡:
jQuery ->
$('#document_docuemnt_lines_attributes_itemno').parent().hide()
itemno = $('#document_docuemnt_lines_attributes_itemno').html()
console.log(itemno)
$('#document_docuemnt_lines_attributes_porhseq').change ->
type = $('#document_docuemnt_lines_attributes_porhseq :selected').text()
options = $(itemno).filter("optgroup[label=#{type}]").html()
console.log(options)
if options
$('#document_docuemnt_lines_attributes_itemno').html(options)
$('#document_docuemnt_lines_attributes_itemno').parent().show()
else
$('#document_docuemnt_lines_attributes_itemno').empty()
$('#document_docuemnt_lines_attributes_itemno').parent().hide()