append在单个对象上执行多次


jQuery .append executes multiple times on single object

我想使用jQuery.append将一个div插入到另一个父div中,该父div是列表元素的一部分,由一个动态创建的表单字段组成。尽管父类只包含一个对象(即文本字段),并且在整个文档中只存在一次,但是.append添加了六次div。它可以在JSfiddle中找到,但不能在我的本地代码(Magento形式)中找到。为什么?这可能与父类的动态创建有关吗?

PHP

$input_name     = $this->getInputName();
$input_id       = $this->getInputId();
$input_value    = $this->getValue();
$input_class    = $this->getInputClass();
$label          = $this->getLabel();
<div class="input-box <?php echo $input_id;?>">
 <input type="text" name="<?php echo $input_name;?>" id="<?php echo $input_id;?>" value="<?php echo $this->htmlEscape($input_value) ?>" title="<?php echo $this->__($label); ?>" class="<?php echo $input_class;?>" />
</div>

使用jQuery

$(".input-box.billing_postcode") .append("<div>My Link</div>")

总是导致以下HTML

<li>
<div class="input-box billing_postcode">
<input id="billing_postcode" class=" input-text required-entry absolute-advice " type="text" title="ZIP" value="" name="billing[postcode]">
<div>My Link</div>
<div>My Link</div>
<div>My Link</div>
<div>My Link</div>
<div>My Link</div>
<div>My Link</div>
</div>

这应该修复:

var $element = $(".input-box.billing_postcode");
if($('.foo', $element).length == 0) {
    $element.append('<div class="foo">My Link</div>');
}