【发布时间】:2012-12-02 12:11:01
【问题描述】:
我已经实现了 jquery 克隆和删除。当点击 ADD 按钮时,带有某些表单元素的 DIV A 会被克隆并插入到另一个 DIV B 中。在 DIV A 中有一个隐藏的表单按钮 REMOVE。现在我需要在单击添加按钮时仅在克隆中启用删除按钮。 IE;我想让 DIV A 中的表单元素始终隐藏。
这是我的代码。
<div class="rule" id="rule">
<div class="fm-req">
<select name="rulefield" id="rulefield">
<option value="">select</option>
</select>
</div>
<div class="fm-opt" >
<input type="button" class='remove' value="-" style="display:none;"/>
</div>
</div>
<div class="fm-rulebutton">
<input type="button" id="addButton "class='add' value="+"/>
</div>
<div id='TextBoxesGroup' class="group">
这里 Div 'rule' 被克隆到 Div 'TextBoxesGroup'
$(document).ready(function() {
var id = 0;
$('.add').click(function(){
id++;
$('.fm-opt').children('.remove').show();
var prot = $(document).find(".rule").clone();
prot.attr("class", 'rule'+id)
prot.find(".id").attr("value", id);
$(document).find("div .group").append(prot);
});
$('.remove').live("click", function(){
$(this).closest("#rule").remove();
});
});
【问题讨论】:
-
我猜这行是指我们在您的代码中看不到的东西:
prot.find(".id").attr("value", id);。我还看到您始终保持相同的 id 但更改班级,这是不正确的。 ID 应该是唯一的,而多个元素的 CLASS 可以是相同的。