2015-08-24 25 views
2

我有一個帶有feeSetting id的表。它包含動態數據,我可以添加和刪除刪除行到表。我動態地生成了ids,它可以正常工作,直到用戶刪除一行並再次添加新行,它會覆蓋最後一行的唯一標識。我想要表格通過動態添加和刪除功能來生成唯一的ID。我添加行的代碼如下。從表中刪除行後,動態ID不起作用

<tablen id="feeSetting "> 
<tbody> 
</tbody> 
</table> 

<script> 
    function AddRow() { 
      debugger; 
      var index = 0; 
      if ($("#feeSetting tbody tr").length > 0) { 
        index = $("#feeSetting tbody tr").length; 

      } 

      $("#feeSetting tbody").append("<tr class='gradeX'>" 

       + "<td class='col-md-3'><input type='text' value='' class='form-control validate[required,custom[number]] text-input txtFromDay' id='eFromDay'/></td>" 
       + "<td class='col-md-3'><input type='text' class='form-control validate[required,custom[number],min[1]] text-input txtValue' value='' id='eValue-" + index + "'/></td>" 
       + "<td class='col-md-4'>" 
       + "<div id='loadTypes-" + index + "' class='typeValidation'></div></td>" 
       + "<td class='col-md-2'><input type='button' class='btn btn-danger btn-sm' value=' Remove '/></td>" 
       + "</tr>"); 
      renderPartialInDiv('@Url.Action("GetValidationTypeDropDown", "FeeFineSetting")?strDDName=eValidationTypeList-' + index + '&intDDID=0&intValidationID=1', '#loadTypes-' + index); 
      $('#eValidationTypeList-'+index).select2(); 
     }; 
</script> 
+0

您可以生成隨機的動態和唯一ID。如果你需要我可以幫助你。 –

+0

我怎麼能訪問然後當我需要發送數據到服務器 –

+0

確認我第一,它需要發送tr長度在這裏,或者只是div ID? renderPartialInDiv('@ Url.Action(「GetValidationTypeDropDown」,「FeeFineSetting」)?strDDName = eValidationTypeList-'+ index +'&intDDID = 0&intValidationID = 1','#loadTypes-'+ index); –

回答

3

嘗試使用一個全局變量,這將增加其上每增加一個新行的值,請參見下面的代碼

<tablen id="feeSetting "> 
<tbody> 
</tbody> 
</table> 

<script> 
    //keep this variable outside function and use it as global variable. 
    var index = 0; 
    function AddRow() { 
      debugger; 
      index++; 
      $("#feeSetting tbody").append("<tr class='gradeX'>" 

       + "<td class='col-md-3'><input type='text' value='' class='form-control validate[required,custom[number]] text-input txtFromDay' id='eFromDay'/></td>" 
       + "<td class='col-md-3'><input type='text' class='form-control validate[required,custom[number],min[1]] text-input txtValue' value='' id='eValue-" + index + "'/></td>" 
       + "<td class='col-md-4'>" 
       + "<div id='loadTypes-" + index + "' class='typeValidation'></div></td>" 
       + "<td class='col-md-2'><input type='button' class='btn btn-danger btn-sm' value=' Remove '/></td>" 
       + "</tr>"); 
      renderPartialInDiv('@Url.Action("GetValidationTypeDropDown", "FeeFineSetting")?strDDName=eValidationTypeList-' + index + '&intDDID=0&intValidationID=1', '#loadTypes-' + index); 
      $('#eValidationTypeList-'+index).select2(); 
     }; 
</script>