Javascript 重复表单字段按钮的问题
我是jquery的新手,在正确克隆具有自动递增id、for和name属性的表单元素块时遇到困难。我正在克隆Javascript 重复表单字段按钮的问题,javascript,jquery,forms,repeat,Javascript,Jquery,Forms,Repeat,我是jquery的新手,在正确克隆具有自动递增id、for和name属性的表单元素块时遇到困难。我正在克隆div.repeatingSection,理想情况下,我只想克隆克隆的部分上的“删除”按钮 到目前为止,我已经设法让它工作,但按钮本身是克隆的(尽管我认为.clone(false)应该可以防止这种情况发生)。我还得到了以下错误:uncaughttypeerror:Object[Object Object]没有方法'live',这可能就是它不能正常工作的原因 HTML <fieldset
div.repeatingSection
,理想情况下,我只想克隆克隆的部分上的“删除”按钮
到目前为止,我已经设法让它工作,但按钮本身是克隆的(尽管我认为.clone(false)
应该可以防止这种情况发生)。我还得到了以下错误:uncaughttypeerror:Object[Object Object]没有方法'live'
,这可能就是它不能正常工作的原因
HTML
<fieldset>
<legend>Expérience professionnelle</legend>
<div class="row repeatingSection">
<div class="large-10 small-12 columns panel inside">
<div class="large-6 columns">
<label for="poste_1">Poste :</label>
<input type="text" name="poste_1" id="poste_1"/>
<div class="row">
<div class="large-6 columns">
<label for="de_1">De :</label>
<input type="date" name="de_1" id="de_1" class="small"/>
</div>
<div class="large-6 columns">
<label for="a_1">A :</label>
<input type="date" name="a_1" id="a_1" class="small"/>
</div>
</div>
<label for="contrat_1">Contrat :</label>
<select name="contrat_1" ID="contrat_1" class="small">
<option></option>
<option value='CDI'>CDI</option>
<option value='CDD'>CDD</option>
<option value='stagiaire'>Stagiaire</option>
<option value='saisonnier'>Saisonnier</option>
</select>
</div>
<div class="large-6 columns">
<label for="description_1"> Description: </label>
<textarea id="description_1" name="description_1" class="lrg-txtarea"></textarea>
</div>
</div><!-- end repeatingSection -->
<div class="large-2 small-12 columns">
<button class="cloneButton small secondary radius indent">Ajouter +</button>
<button class="removeButton small secondary radius indent">Enlever -</button>
</div>
任何帮助都将不胜感激。
下面是一个JSAJSIDLE:自JQ1.9以来,使用
.on()
来委托事件:{或者您可以使用.delegate()
方法}
jQuery(document.body).on('click','.removeButton', function(){...});
嗨,我相信你的任务已经完成了检查这把小提琴
根据您使用的jq版本(您的JSFIDLE没有提供信息!!!),live()已被删除(JQ1.9),请使用.on()或其1.10.2版本(必须与Wordpress 3.6配合使用)。好吧,那就解释了错误。好多了。谢谢如何仅克隆附加字段上的“删除”按钮而不克隆“添加”按钮?要从克隆中删除特定元素:
lastreatinggroup.clone(false)。find('.cloneButton')。remove().end()代码>也就是说,因为您似乎只想克隆一个元素,然后直接克隆它,而不是它的父容器。不,我确实想克隆div中的所有元素,而不仅仅是按钮。啊,我现在明白您的意思了。如果我填写了该部分,然后点击add按钮,我将复制表单字段及其内容。而不是一张空表格。它看起来更整洁。我唯一的问题是我理解我正在使用的,而不是你建议的,我还需要为
属性增加名称
和。实际上,我想克隆“删除”按钮,但不想克隆“添加”按钮。不过谢谢你,我会把它收藏起来,等我更好地理解它的时候。
jQuery(document.body).on('click','.removeButton', function(){...});
$("button.clone").live("click", function(){
$(this).parents(".clonedInput").clone()
.appendTo("body")
.attr("id", "clonedInput" + cloneIndex)
.find("*").each(function() {
var id = this.id || "";
var match = id.match(regex) || [];
if (match.length == 3) {
this.id = match[1] + (cloneIndex);
}
});
cloneIndex++;
});