2017-02-16 3 views
0

クローンされた のものが機能していないため、新しい行をクローンしたときにSelect2が機能しなくなりました。 "ドロップダウンメニューの操作 最初の行(デフォルトのもの)のみ"ドロップダウンメニュー。ここに私のコードは次のとおりです。select2は複製された行に対して機能しません。なぜですか?

**= f.inputs 'Items not in inventory' do 
    %table 
     %thead 
     %td{style: 'width: 20%; padding-left: 20px;'} 
      %b Select Product 
     %td{style: 'max-width: 70px;'} 
      %b Repossessed Inventory (Serial/Amount) 

     %tbody{id: 'tableToModify'} 
     %tr{id: 'rowToClone'} 
      = semantic_fields_for :other_stuff do |os| 
      %td{style: 'padding-left: 20px;'} 
       = os.input :product_id, as: :select, label: false, collection: Product.all.order(:name).pluck(:name, :id), input_html: {class: 'select2'} 
      %td 
       = os.input :repossessed, label: false, input_html: { style: 'width: 100px;' } 
       %td 

       = link_to "x", "javascript:if ($('#tableToModify tr').length > 1) {document.getElementById('tableToModify').deleteRow($('#tableToModify tr').length - 1);}" 

    = link_to 'Add item', 'javascript:cloneRow()' 
    = f.input :rest_is_lost, as: :boolean, label: 'Mark unrepossessed items as lost', input_html: { checked: 'checked' } 

    = f.actions do 
    = f.action :submit, label: 'Create Repossession Case' 
    = f.action :cancel, label: 'Cancel', button_html: { href: url_for(action: :index) }, wrapper_html: { class: :cancel } 

:javascript 
    function cloneRow(){ 
    var row = document.getElementById("rowToClone"); 
    var table = document.getElementById("tableToModify"); 
    var clone = row.cloneNode(true); 
    var numberOfRows = document.getElementById("tableToModify").rows.length; 
    clone.id = numberOfRows + 1; 
    table.appendChild(clone); 
    }** 

答えて

0

あなたは、行のクローンを作成し、再度バインドする前に、SELECT2を破壊する必要があります。私がしようと、

function cloneRow(){ 
    var row = document.getElementById("rowToClone"); 
    var table = document.getElementById("tableToModify"); 

    $(table).find('select').select2('destroy'); 

    var clone = row.cloneNode(true); 
    var numberOfRows = document.getElementById("tableToModify").rows.length; 
    clone.id = numberOfRows + 1; 
    table.appendChild(clone); 

    $(table).find('select').select2(); 
} 
+0

は先端のためにあなたをとても感謝が、これはあまりにも機能していません入力内の要素を取得し、select2でバインドされていますが、何度も失敗します。どうしてか分かりません!!! –

+0

それは私にこのエラーを与えます:clone.getElementByIdは関数ではありません –

+0

'console.log(clone)'を使って 'clone'を確認します。 –

関連する問題