【问题标题】:Repeater field groups中继器字段组
【发布时间】:2013-11-18 19:00:27
【问题描述】:

我正在尝试将一组转发器字段添加到 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 
}

【问题讨论】:

    标签: php jquery wordpress field repeater


    【解决方案1】:

    我已经找到了问题所在。保存后,它正在为组中的每个选项重复字段组。我通过将可重复部分包装在条件语句中来解决问题,检查第一个必填字段是否为空值:

    if ( $options['ssfrm_mytext'.$i] !== null ) {
    
       // repeatable fields section 
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2014-04-02
      • 2019-03-30
      • 2017-01-01
      • 1970-01-01
      • 2018-04-11
      • 2014-09-22
      • 2018-07-17
      • 2020-06-14
      相关资源
      最近更新 更多