2015-06-14 55 views
0

我有一張表,需要重複行和增加創建的新行的id。我想這一切都可以用JQUERY來完成,但是這不是很好... 發現堆棧溢出的例子似乎不適用於我。 感謝您的幫助Jquery增加ID的重複錶行

<table width="100%" id="table-data" > 
    <tr> 
    <td><strong>Title</strong></td> 
    <td><input id="input_1" type="text" style="width:200px" value="Nom"/> 
     <input type="text" style="width:200px" value="Prénom"/> 
<a href="#"><i class="icon-plus"></i>ADD</a></td> 
    </tr> 

    <tr> 
    <td valign="top"><strong>Title</strong></td> 
    <td > 
     <div id="bloc_cell"> 
     <input id="bloc_1" type="text" class="requis" value="Name"/> 
     <br> 
     <TEXTAREA class="requis" name="positive3" >Adress</TEXTAREA> 
     <br> 
     <input type="text" value="Tél"/> 
     <br> 
     <input type="text" value="Email"/> 
     <br> 
     <input type="text" value="Web"/> 
     </div> 
     <a href="#"><i class="icon-plus">ADD</i></a></td> 
    </tr> 

    </table>  
+0

您的意思是添加新行嗎? –

+0

是添加新行,克隆以前的內容,但遞增ID = ... – pipoulito

+0

重複哪一行?什麼時候?增加哪個ID?你有什麼嘗試? – MrUpsidown

回答

0

其實,我找到了解決這個jsfiddle

但我只需要添加一個刪除按鈕,即去除TR增加。 此刪除按鈕應該只出現,如果從第二個TR(第一行不能刪除) 感謝您的幫助!

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data"> 
    <tr> 
     <td>Name</td> 
     <td>Location</td> 
     <td>From</td> 
     <td>To</td> 
     <td>Add</td> 
    </tr> 
    <tr id="id1" class="tr_clone"> 
     <td><input type="text" autofocus placeholder="who" name="who" id="who1" ></td> 
     <td><select name="txtCategory[]" id="category1"> 
      <option value="">Please select</option> 
     </select></td> 
     <td><input type="text" placeholder="Start Date" name="datepicker_start" class="datepicker" id="datepicker_start1"></td> 
     <td><input type="text" placeholder="End Date" name="datepicker_end" class="datepicker" id="datepicker_end1"></td> 
     <td><input type="button" name="add" value="Add" class="tr_clone_add"></td> 
    </tr> 
</table><!-- /table#table-data --> 


var regex = /^(.*)(\d)+$/i; 
var cindex = 1; 

$("input.tr_clone_add").on('click', function() { 
    var $tr = $(this).closest('.tr_clone'); 
    var $clone = $tr.clone(true); 
    cindex++; 
     $clone.find(':text').val(''); 

    //update ids of elements in row 
    $clone.find("*").each(function() { 
      var id = this.id || ""; 
      var match = id.match(regex) || []; 
      if (match.length == 3) { 
       this.id = match[1] + (cindex); 
      } 
    }); 
    $tr.after($clone); 
});