Jquery 对动态添加的文本框重新编号

Jquery 对动态添加的文本框重新编号,jquery,coldfusion,Jquery,Coldfusion,我有下面的代码,可以动态地在表单中添加和删除文本框 $(document).ready(function(){ var str = '<div id="my{0}Div"><textarea id="corrective{0}" name="corrective{0}" rows="12" cols="50" class="field textarea small"></textarea><span class="link" id="delet

我有下面的代码,可以动态地在表单中添加和删除文本框

$(document).ready(function(){    
var str = '<div id="my{0}Div"><textarea id="corrective{0}" name="corrective{0}"  rows="12" cols="50" class="field textarea small"></textarea><span class="link" id="delete{0}Row">Remove This</span></div>';

<cfif thisInstance.recommendationCorrectiveAction.RecordCount>
var i = <cfoutput>#thisInstance.recommendationCorrectiveAction.RecordCount#</cfoutput>+1;
<cfelse>
var i = 2;                              
</cfif>

 function addRow() {
    updateStr = jQuery.format(str, i);                              
    $(updateStr).appendTo("#MyRecommends");

     // add the click handler for the delete link
    $('#delete'+i+'Row').click(deleteRow);

   // reset the corrective action count
    updateCorrectiveActionCount = parseInt(objForm.correctiveActionCount.getValue())+1;
    objForm.correctiveActionCount.setValue(updateCorrectiveActionCount);  
  i++;
 }

function deleteRow() {
    var parentID = $(this).parent('div').attr('id');            
    $('#MyRecommends #'+parentID).remove();          
    // reset the corrective action count
    updateCorrectiveActionCount = parseInt(objForm.correctiveActionCount.getValue())-1;
    objForm.correctiveActionCount.setValue(updateCorrectiveActionCount);
    i--;  
    }


    $("#add").click(addRow);
}); 
$(文档).ready(函数(){
var str='删除此';
var i=#thisInstance.recommendationCorrectiveAction.RecordCount#+1;
var i=2;
函数addRow(){
updateStr=jQuery.format(str,i);
$(updateStr).appendTo(“#myrecomments”);
//为删除链接添加单击处理程序
$(“#删除“+i+”行”)。单击(删除行);
//重置纠正措施计数
updateCorrectiveActionCount=parseInt(objForm.correctiveActionCount.getValue())+1;
objForm.correctiveActionCount.setValue(updateCorrectiveActionCount);
i++;
}
函数deleteRow(){
var parentID=$(this.parent('div').attr('id');
$('#myrecommendes#'+parentID).remove();
//重置纠正措施计数
updateCorrectiveActionCount=parseInt(objForm.correctiveActionCount.getValue())-1;
objForm.correctiveActionCount.setValue(updateCorrectiveActionCount);
我--;
}
$(“#添加”)。单击(添加行);
}); 
如果我连续添加和删除文本框,效果很好。但如果我添加两个文本框,分别是tb2和tb3,然后按tb2的“删除此”链接;虽然上面i的值减少为2,但我得到一个错误,即“Element corrective2在类型为coldfusion.filter.FormScope的Java对象中未定义。
错误发生在第351行。”


我需要一个解决方案,每次添加或删除文本框时,整个系列都会重新编号,或者任何更好的解决方案。

您在重新编号方面做了哪些尝试?这与其说是ColdFusion问题,不如说是一个JQuery问题,因为重新编号需要在客户端进行,而ColdFusion在这方面不起作用。我不知道如何对元素重新编号。我的一位同事提出了这个建议,但我不完全确定该怎么做或在哪里做。哪一部分?如何更改元素的ID?如何循环?如何使用JQuery或Javascript?如果您不知道如何执行此操作,那么您可能会过早地提出有关S/O的问题。在寻求他人帮助之前,你需要在解决问题时表现出一定的努力。你对此做了哪些研究?你明白你贴的代码吗?不仅仅是最终的结果,还有它是如何实现的?@AdamCameron我理解上面的代码,但我不确定如何循环,以便可以对div中的元素重新编号。我需要deleteRow函数中类似于for循环的东西,它只有在parentID小于I(我认为)时才起作用。我对jQuery非常陌生,对所有函数都不了解,我自己编写了deleteRow函数,并认为它工作得很好,直到有人指出上面提到的问题。这就是我来这里寻求帮助的原因。