rails on rails动态选择cocoon嵌套子表单中的表单

时间:2017-06-22 14:51:34

标签: jquery ruby-on-rails cocoon-gem

我正在使用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()

0 个答案:

没有答案