2011-12-10 18 views
1

jqueryを使用してラジオグループを複製しようとしています。私のクローニングコードは次のとおりです。jquery clone id update

$("#btnAddAnotherLocation").live("click", function(){ 
    cloneIndexLocation++; 

    $("#clonedInputLocation0").clone() 
    .appendTo("#clonedInputsLocation") 
    .attr("id", "clonedInputLocation" + cloneIndexLocation) 
    .find("*").each(function(){ 
     var id = this.id || ""; 
     var match = id.match(regex) || []; 

     if(match.length == 5){ 
      this.id = match[1]+(cloneIndexLocation); 
     } 
    }).end(); 
}); 

マイcloneDivの内容は次のとおりです。

はそれが厄介探しについて
<div id="clonedInputLocation0" class="clonedInputLocation" style="display: none;"> 
    <div class="formLocationWrapper"> 
     <label class="labelRadioBlack labelContainsInput" for="locationInNear0"> 
      <input name="locationInNearRadio[]" id="locationInNear0" value="" type="radio" /> 
      In or near 
     </label> 
     <div class="inNearTextWrapper"> 
     <input class="myWirrlMediumFormField" type="text" name="txtLocationInNear[]" value="" /> 
    </div> 
</div> 
<div class="formLocationWrapper"> 
    <label class="labelRadioBlack labelContainsInput" for="locationAnywhereIn0"> 
     <input name="locationInNearRadio[]" id="locationAnywhereIn0" value="" type="radio" checked="checked" /> 
       Anywhere in 
    </label> 
</div> 
    <div class="" id="clonedInputLocationRemove0"> 
     <a href="javascript: void(0);">Remove location</a> 
    </div> 
</div> 

申し訳ありませんが、それは私のエディタでたくさんクリーナー実際にあります。

問題は実際のコンテンツを複製することではなく、うまくいきます。問題は、ラジオグループ名をlocationInNearRadio [cloneIndexLocation]に更新し、IDとFOR属性の名前をlocationInNearcloneIndexLocationとし、cloneIndexLocationが次の増分である必要があることです。

このコードは、すべてのIDをclonedIndex番号に更新する選択フィールドのグループに対して機能しますが、何らかの理由で同じコードを取得してラジオグループIDを更新することさえできません。

乾杯

+0

何をあなたの正規表現は次のようになり、あなたが文字列で1を使用して、番号をインクリメントまたは単にIDの末尾に「1」を追加していることを確信しているのでしょうか? jsFiddle.netのデモがはるかに役立つでしょう。 – Mottie

答えて

0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var cloneIndexLocation = 0; 
      var clonedInputLocation = $('<div></div>').append($("#clonedInputLocation0").clone()).html(); 


      $("#btnAddAnotherLocation").live("click", function() { 
       //cloneIndexLocation++; //If u remove the locatoin then index has to be changed. 

       //Or 

       cloneIndexLocation = $('#clonedInputsLocations div').length; 

       $(clonedInputLocation.replace(/collectionIndex/gi, cloneIndexLocation)).appendTo("#clonedInputsLocations"); 
       $('#clonedInputsLocations div').show(); 
      }); 
     });  
    </script> 
</head> 
<body> 
    <!--where you need to replace by cloneIndexLocation use (collectionIndex) word in there. it wiil replace it by number.--> 
    <div id="clonedInputLocation0" class="clonedInputLocation" style="display: none;"> 
     <div class="formLocationWrapper"> 
      <label class="labelRadioBlack labelContainsInput" for="locationInNear0"> 
       <input name="locationInNearRadio[collectionIndex]" id="locationInNear0" value="" 
        type="radio" /> 
       In or near 
      </label> 
      <div class="inNearTextWrapper"> 
       <input class="myWirrlMediumFormField" type="text" name="txtLocationInNear[collectionIndex]" 
        value="" /> 
      </div> 
     </div> 
     <div class="formLocationWrapper"> 
      <label class="labelRadioBlack labelContainsInput" for="locationAnywhereIn0"> 
       <input name="locationInNearRadio[collectionIndex]" id="locationAnywhereIn0" value="" 
        type="radio" checked="checked" /> 
       Anywhere in 
      </label> 
     </div> 
     <div class="" id="clonedInputLocationRemove0"> 
      <a href="javascript: void(0);">Remove location</a> 
     </div> 
    </div> 
    <input type="button" id="btnAddAnotherLocation" value="AddAnotherLocation" /> 
    <div id="clonedInputsLocations"> 
    </div> 
</body> 
</html> 
+0

ライブデモはhttp://jsfiddle.net/nanoquantumtech/FSBdY/ – Thulasiram

+0

ファイヤーフォックスのファーストインスタントFirebugアドオンです。 Firefoxのブラウザでコードを実行します。 f12を押すと、変更されたcollectionIndexが整数に表示されます。 – Thulasiram