我对 JQuery 相当陌生,我正在尝试通过单击按钮添加多个表单字段。虽然我能够删除现有字段,但我无法添加新字段。
测试:http: //jsfiddle.net/Z4kYf/
我的 HTML 代码是:
<div class="repeatable-wrap">
<ul id="tracks-repeatable" class="repeatable-fields-list">
<li>
<input type="text" name="fr_options1" value="" size="200" />
<a class="repeatable-field-remove button" href="#">REMOVE</a>
</li>
</ul>
</div>
<a class="repeatable-field-add button" href="#">ADD</a>
和jQuery:
jQuery(function(jQuery) {
jQuery('.repeatable-field-add').click(function() {
var theField = jQuery(this).closest('div.repeatable-wrap').find('.repeatable-fields-list li:last').clone(true);
var theLocation = jQuery(this).closest('div.repeatable-wrap').find('.repeatable-fields-list li:last');
jQuery('input', theField).val('').attr('name', function(index, name) {
return name.replace(/(\d+)/, function(fullMatch, n) {
return Number(n) + 1;
});
});
theField.insertAfter(theLocation, jQuery(this).closest('div.repeatable-wrap'));
var fieldsCount = jQuery('.repeatable-field-remove').length;
if (fieldsCount > 1) {
jQuery('.repeatable-field-remove').css('display', 'inline');
}
return false;
});
jQuery('.repeatable-field-remove').click(function() {
jQuery(this).parent().remove();
var fieldsCount = jQuery('.repeatable-field-remove').length;
if (fieldsCount == 1) {
jQuery('.repeatable-field-remove').css('display', 'none');
}
return false;
});
});
</p>