编辑 我都尝试下面的建议,但它仍然没有工作...它的克隆,但不增加......jQuery的克隆格查找和替换到处
功能createNewSection() {
var cloneUp = $('.section-form').length;
var cloneCount = $('.section-form').length - 1;
var newSection = $('#section-0').clone(function() {
$(this).find('*').each(function() {
$(this).attr('id', $('#section-0').attr('id').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
$(this).attr('name', $('#section-0').attr('replace').replace(/section-0/,'section-'+cloneUp).replace(/section[_\[]0\]?/,'section_'+cloneUp));
});
});
$('#sections').append(newSection);
}
我试图替换特定字符串的所有实例,已经尝试了多种方法来做到这一点,但往往不是,我得到一个错误说代替ISN”一个func重刑,$(这)是不确定的,或丢失:争吵后...
例如,在下面的HTML:
<div id="section-1-add">
<div id="section-1">
<span class="section_1-span">Title</span>
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1/Item 1" />
</div>
</div>
我想要替换的所有实例:
section-1 with section-2
section_1 with section_2
section[1] with section_2
我试过的实质上是一样的许多变化,但这里是最近的尝试:
function createNewSection() {
var cloneUp = 2; //this is currently hardcoded, but will be dynamic... just for this example
var newSection = $('#section-1-add').clone(function() {
$(this).find('*').each(function() {
$(this).id=$(this).id.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
$(this).name=$(this).name.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
});
});
$('#sections').append(newSection);
}
我上述N码,部分被克隆,但没有一个ID或名称的改变......
我相信你也可以用'this'而不是'$(this)[0]' –