如何使用克隆的jQuery对象为div内容中的每个id属性添加增量值
很难确定如何使用克隆的jQuery对象为div内容中的每个id属性添加增量值。
http://jsfiddle.net/hvK8d/
===================== HTML =====================
===================== JQUERY =====================
//Cloning upload file control $('.remove').live('click', function () { if (confirm("Are you sure you wish to remove this item?")) { $(this).parent().slideUp('fast', function () { $(this).remove(); }); } return false; }); $('.plus').click(function () { console.log('cloning'); var divCloned = $('.upload-file-container:first').clone(); divCloned.hide().insertAfter('.upload-file-container:last').slideDown('fast'); return false; });
为了完整起见,我将在这里提出一个使用“模板”的小解决方案。
隐藏模板的类:
.upload-file-container.template { display: none; }
一个小function来做替换:
$.fn.template = function(variables) { return this.each(function() { this.innerHTML = this.innerHTML.replace(/{{(.+)}}/g, function(match, variable) { return variables[variable]; }); return this; }); };
模板:
用法:
var count = 0; var divCloned = $(".upload-file-container.template") .clone() .removeClass("template") .template({ id: count++ });
您应该在name
属性中使用类似数组的表示法(例如RadioButtonList[]
),而不是使用编号ID,并在input
s周围包装标签:
PS您还应该考虑使用比RadioButtonList更具描述性的名称。