Javascript 从表中删除行后,动态id不起作用
我有一个带有feeseting id的表。它包含动态数据,我可以在表中添加和删除行。我动态生成的id工作正常,直到用户删除一行并再次添加新行,它会覆盖最后一行的唯一id。我希望表生成具有动态添加和删除功能的唯一id。我添加行的代码如下所示Javascript 从表中删除行后,动态id不起作用,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个带有feeseting id的表。它包含动态数据,我可以在表中添加和删除行。我动态生成的id工作正常,直到用户删除一行并再次添加新行,它会覆盖最后一行的唯一id。我希望表生成具有动态添加和删除功能的唯一id。我添加行的代码如下所示 <tablen id="feeSetting "> <tbody> </tbody> </table> <script> function AddRow() { d
<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>
函数AddRow(){
调试器;
var指数=0;
如果($(“#feesettbody tr”)。长度>0){
索引=$(“#feetsetting tbody tr”)。长度;
}
$(“#feestettbody”)。追加(“”
+ ""
+ ""
+ ""
+ ""
+ ""
+ "");
renderPartialInDiv('@Url.Action(“GetValidationTypeDropDown”,“FeeFineSetting”)?strDDName=eValidationTypeList-'+index+'&intDDID=0&intValidationID=1','#loadTypes-'+index);
$('#eValidationTypeList-'+索引)。选择2();
};
尝试使用一个全局变量,该变量将在添加每一新行时增加其值,请参见下面的代码
<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>
//将此变量保留在函数外部,并将其用作全局变量。
var指数=0;
函数AddRow(){
调试器;
索引++;
$(“#feestettbody”)。追加(“”
+ ""
+ ""
+ ""
+ ""
+ ""
+ "");
renderPartialInDiv('@Url.Action(“GetValidationTypeDropDown”,“FeeFineSetting”)?strDDName=eValidationTypeList-'+index+'&intDDID=0&intValidationID=1','#loadTypes-'+index);
$('#eValidationTypeList-'+索引)。选择2();
};
您可以生成随机的动态唯一id。如果需要,我可以帮助您。当我需要向服务器发送数据时,如何访问?首先确认我,需要在此处发送tr长度,或者仅发送div id?renderPartialInDiv('@Url.Action(“GetValidationTypeDropDown”,“FeeFineSetting”)?strDDName=eValidationTypeList-'+index+'&intDDID=0&intValidationID=1','#loadTypes-'+index);只需div id,它取决于长度。@BhushanKawadkar使用了正确的方法!