当使用 Javascript 动态添加包含在单个字段集中的其他表单字段时,我遇到了关闭字段集标记仍然在第一个表单字段之后应用的问题。这导致布局中断,我需要弄清楚如何解决这个问题。
我试图将关闭字段集标记移到附加字段的 DIV 之外,但 Firebug 检查仍将其显示为在第一项之后关闭。
JAVASCRIPT
<script type="text/javascript">
$(function()
{
var template = $('#inventoryItems .inventory:first').clone(),
inventoryCount = 1;
var addInventory = function()
{
inventoryCount++;
var inventory = template.clone().find(':input').each(function()
{
var newId = this.id.substring(0, this.id.length-1) + inventoryCount;
$(this).prev().attr('for', newId); // update label for (assume prev sib is label)
this.name = this.id = newId; // update id and name (assume the same)
}).end() // back to .attendee
.attr('id', 'inv' + inventoryCount) // update attendee id
.appendTo('#inventoryItems'); // add to container
};
$('.btnAddInventory').click(addInventory); // attach event
});
</script>
HTML
<div id="inventoryItems" class="inventoryItems" style="margin:0; padding:0;">
<fieldset style="width:62%; float:left; margin-left: 19%;">
<div id="inv1" class="inventory">
<label>Inventory</label>
<select name="invItem" style="width:92%;">
<?php
$invItem_values = array("id", "name");
display_options_list($dp_conn, $invItem_values, "inventory", "id");
?>
</select>
<a class="btnAddInventory"><img src="images/icn_new_article.png"></a>
<a href="#"><img src="images/icn_trash.png"></a>
</div>
</div>
</fieldset><div class="clear"></div>