我有以下代码,它动态地添加和删除表单的文本框。
$(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);
});
如果我连续添加和删除文本框,这可以正常工作。但如果我添加两个文本框说tb2和tb3并按下“删除此”链接为tb2;虽然上面的i的值减少到2但是我得到一个错误,说“元素corrective2在类型类coldfusion.filter.FormScope类型的Java对象中未定义。&lt; br&gt;错误发生在第351行。”
我需要一个解决方案,每次添加或删除文本框时,整个系列都会重新编号或更好的解决方案。