jqueryのクローンと削除を実装しました。[追加]ボタンをクリックすると、特定のフォーム要素を持つDIV Aが複製され、別のDIVBに挿入されます。DIV Aには、非表示のフォームボタンREMOVEがあります。ここで、[追加]ボタンがクリックされたときに、クローンでのみ[削除]ボタンを有効にする必要があります。すなわち; DIVAのフォーム要素を常に非表示にしておきたい。
これは私のコードです。
<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'ルール'は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();
});
});