我正在尝试将一组重复器字段添加到 WordPress 插件的设置页面。如果我只有一个中继器字段,则此代码有效,但如果同一组中有多个中继器字段,则其行为会出乎意料。它的作用是,保存设置后,它会自动添加空字段。例如,如果组中有两个中继器字段,则保存后它将添加两个空字段。如果我有两个以上的中继器字段,则保存后空字段的数量会呈指数级增长。我不明白它为什么这样做。同样,使用当前代码,如果我仅使用一个中继器字段,则保存后不会添加任何空字段(这就是我想要的)。
这是我正在使用的代码:
function ssfrm_render_form(){ ?>
<form method="post" action="options.php">
<?php settings_fields('ssfrm_plugin_options'); $options = get_option('ssfrm_options'); ?>
<script>
jQuery(document).ready(function($) {
$('.repeatable-field-add').click(function() {
var theField = $(this).closest('div.repeatable-wrap')
.find('.repeatable-fields-list li:last').clone(true);
var theLocation = $(this).closest('div.repeatable-wrap')
.find('.repeatable-fields-list li:last');
$('input', theField).val('').attr('name', function(index, name) {
return name.replace(/(\d+)/, function(fullMatch, n) {
return Number(n) + 1;
});
});
$('select', theField).val('').attr('name', function(index, name) {
return name.replace(/(\d+)/, function(fullMatch, n) {
return Number(n) + 1;
});
});
theField.insertAfter(theLocation, $(this).closest('div.repeatable-wrap'));
var fieldsCount = $('.repeatable-field-remove').length;
if( fieldsCount > 1 ) {
$('.repeatable-field-remove').css('display','inline');
}
return false;
});
$('.repeatable-field-remove').click(function(){
$(this).parent().remove();
var fieldsCount = $('.repeatable-field-remove').length;
if( fieldsCount == 1 ) {
$('.repeatable-field-remove').css('display','none');
}
return false;
});
});
</script>
<h4>Configure PDF Output</h4>
<?php
echo '<div class="repeatable-wrap"><ul id="tracks-repeatable" class="repeatable-fields-list">';
if ( ! empty( $options ) ) {
$i = 1;
foreach( $options as $option ) {
?> <li>
<input type="text" name="ssfrm_options[ssfrm_mytext<?php echo $i; ?>]" value="<?php echo $options['ssfrm_mytext'.$i]; ?>" />
<input type="text" name="ssfrm_options[ssfrm_myothertext<?php echo $i; ?>]" value="<?php echo $options['ssfrm_myothertext'.$i]; ?>" />
<select name="ssfrm_options[ssfrm_myselect<?php echo $i; ?>]">
<option value="" <?php selected('', $options['ssfrm_myselect'.$i]); ?>></option>
<option value="true" <?php selected('true', $options['ssfrm_myselect'.$i]); ?>>Yes</option>
<option value="false" <?php selected('false', $options['ssfrm_myselect'.$i]); ?>>No</option>
</select>
<a class="repeatable-field-remove button" href="#">X</a>
</li>
<?php
$i++;
}
} else {
?> <li>
<input type="text" name="ssfrm_options[ssfrm_mytext1]" value="<?php echo $options['ssfrm_mytext1']; ?>" />
<input type="text" name="ssfrm_options[ssfrm_myothertext1]" value="<?php echo $options['ssfrm_myothertext1']; ?>" />
<select name="ssfrm_options[ssfrm_myselect1]">
<option value="" <?php selected('', $options['ssfrm_myselect1']); ?>></option>
<option value="true" <?php selected('true', $options['ssfrm_myselect1']); ?>>Yes</option>
<option value="false" <?php selected('false', $options['ssfrm_myselect1']); ?>>No</option>
</select>
<a class="repeatable-field-remove button" href="#">X</a>
</li>
<?php
} ?>
</ul><a class="repeatable-field-add button" href="#">+</a></div>
<br />
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
</form>
</div>
<?php
}
我已经找到问题所在了。保存后,它会为组中的每个选项重复字段组。我通过将可重复部分包装在条件语句中,检查第一个必填字段是否具有空值来解决这个问题:
if ( $options['ssfrm_mytext'.$i] !== null ) {
// repeatable fields section
}
您可以参考此文档:https://github.com/wp-add-ons/Gravity-forms-repeater。 对你有帮助