2012-01-19 16 views
2

オートコンプリートクラスを持つテーブルの入力フィールドを複製しています。私がフィールドをクローンするとき、私は問題ありません。問題は、クローンされたフィールドでは、オートコンプリートが動作しないことです(クローンされていないものは動作します)。入力フィールドのクローン作成時にオートコンプリートが機能しない

$(document).ready(function() { 
     $('#btnAdd').click(function() { 
      var num  = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
      var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

      // create the new element via clone(), and manipulate it's ID using newNum value 
      var newElem = $('#input' + num).clone().prop('id', 'input' + newNum); 


      newElem.find(':input').each(function() { 
      var name = $(this).attr('name').replace(/\d+$/, ''); 

      $(this).prop({id: name + newNum, name: name + newNum}).val(""); 


     });    

      // insert the new element after the last "duplicatable" input field 
      $('#input' + num).after(newElem); 

      // enable the "remove" button 
      $('#btnDel').prop('disabled',''); 

      // business rule: you can only add 15 names 
      if (newNum == 15) 
       $('#btnAdd').prop('disabled','disabled'); 
     }); 

     $('#btnDel').click(function() { 
      var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have 
      $('#input' + num).remove();  // remove the last element 

      // enable the "add" button 
      $('#btnAdd').prop('disabled',''); 

      // if only one element remains, disable the "remove" button 
      if (num-1 == 1) 
       $('#btnDel').prop('disabled','disabled'); 
     }); 

     $('#btnDel').prop('disabled','disabled'); 
    }); 

私のオートコンプリートのコードは次のとおりです:私のオートコンプリートのコードはこれです、私はイムがやって何を、イムわからないクローンコードの内部でオートコンプリートのコードを置くことによってそれを修正しようとしてきた

var autoc = { 
     source: "lib/search.php", 
     minLength: 1, 
     select: function(event, ui) { 
      log(ui.item ? 
       "Selected: " + ui.item.value + " aka " + ui.item.id : 
       "Nothing selected, input was " + this.value); 
        } 
       }; 

       var renderItem = function(ul, item) { 
         return $("<li></li>") 
        .data("item.autocomplete", item) 
        .append("<a style='height:75px; text-align:center; font-weight:bold;'>"+ item.label + "</a>") 
        .appendTo(ul); 
       }; 

       $(".member").each(function (i) { 
       $(this).autocomplete(autoc).data("autocomplete")._renderItem = renderItem; 
     }); 

違う。もし誰かが助けてくれれば素晴らしいだろう!ありがとう!

答えて

1

オートコンプリートフィールドは、複製された後に再初期化する必要があります。

$('#my_clone_button').live('click',function() { 
    my_clone_script; #this is my function to clone 

    $('select your new cloned input').each(function() { 
     $(this).autocomplete('destroy'); 
     enable_autocomplete($(this), json_url); #this is my function to initialize autocomplete 
    }); 

}); 
0

try $('#input' + num).clone(true) trueを渡すと、イベントとデータもコピーするようにクローンに指示します。これは、入力がオートコンプリートフィールドであることをコピーすることを意味します。

+0

HI:!そして私はこれまで私のソリューションは、このようなものだっただけでなく

として.live()以内にそれをラップする必要があると思います迅速な答えをありがとう。私は別の部分にそのコードを入れたが、それは仕事をしなかった。 – xionhack