2009-03-10 5 views
3

私はJörnZaeffererのjquery autocompleteプラグインを使用しています。オートコンプリートフィールドをクローンするときに、それを動作させる方法を理解できないようです。クローンされたオートコンプリートフィールドは、テキストを入力すると選択肢が表示されますが、項目を選択することはできません。最初は私はそれがブラウザとの互換性の問題だと思っていましたが、FF3とSafariの両方で発生するので、私が見逃した問題があると思います。ここで JQuery:オートコンプリートフィールドを複製するには?

は私がやっているの実施例である:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<title>Autocomplete Clone Demo</title> 
<style> 
body { 
margin: 40px; 
} 
.hide_element { 
display: none; 
} 
</style> 
<link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" /> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script> 
<script type="text/javascript"> 
    function setAutocomplete() 
    { 
     var users = [ 
      { name: "Fred", id: "1" }, 
      { name: "Barney", id: "2" }, 
      { name: "Wilma", id: "3" } 
     ]; 

     $(".user_selector").autocomplete(users, 
      { 
       mustMatch: true, 
       matchContains: true, 
       minChars: 2, 
       formatResult: function(row) { return row.name; }, 
       formatItem: function(row, i, max) { return row.name; } 
      } 
     ); 
    } 

    var current= 0; 

    var addParticipantFields = function() 
    { 
     current++; 
     $newParticipant = $("#template").clone(true); 
     $newParticipant.removeAttr("id"); 
     $newParticipant.removeClass("hide_element"); 
     $prefix = "extra" + current; 
     $newParticipant.children("div").children(":input").each(function(i) { 
      var $currentElem= $(this); 
      $currentElem.attr("name",$prefix+$currentElem.attr("name")); 
     }); 
     $newParticipant.appendTo("#participantsField"); 
     setAutocomplete(); 
    } 

    $(document).ready(function() { 
     setAutocomplete(); 
     $("#addParticipant").live("click", addParticipantFields); 

    }); 
</script> 

</head> 
<body> 
<h1>Test Autocomplete Cloning</h1> 
<form id="demo" method="post" action=""> 
<fieldset id="participantsField"> 
<label>Participants</label> 
<div class="participant"> 
    <input class="user_selector" name="user" size="30"/> 
</div> 
</fieldset> 

<!-- This is the template for adding extra participants --> 
<div class="participant hide_element" id="template"> 
    <input class="user_selector" name="_user" size="30"/> 
</div> 

<p><input type="button" id="addParticipant" value="Add Another Participant"></p> 
<p><input class="button" type="submit" value="Submit"/></p> 
</form> 
</body> 
</html> 

答えて

3

作るので

$newParticipant = $("#template").clone(); 

あなたの例では、FFで私の作品のような

$newParticipant = $("#template").clone(true); 

ときドン#templateのイベントをクローンしません。すべての

+0

私は理由は分かりませんが、実際にはうまくいきます。前に説明した同じ問題のあるオートコンプリートの動作に遭遇しましたが、この修正を行った後にクローン化されていない入力ボックスには発生しましたが、今は再現できないので、時々イベントが乱れることがあります。 – Rob

+0

に同じ問題がありましたが、イベントハンドラを使用してclone()を実行する必要がありました。回避策はunautocomplete()とautocomplete()を繰り返し、clone()後にすべての要素のすべてのイベントハンドラを再設定するのがはるかに簡単です – CountZero

1

最初:

$newParticipant.children("div").children(":input").length == 0 

ので、このラインで返される子はありません。 用途

$newParticipant.children() 

代わりに、代わりに1つの盾を返します。しかし、鉄は私のために働かない。もっと考えなければならない。

+0

良いキャッチ - それを指摘してくれてありがとう。 – Rob