jQuery clone表单字段组将wp_editor()函数添加到textarea

时间:2017-06-01 08:56:54

标签: javascript php jquery wordpress wp-editor

在CPT元数据框中,我使用的是wp_editor()。第一个是通过php函数加载。但是,当我通过jQuery克隆表单字段时,它不会添加wp_editor而是添加简单的textarea。

所以here我发现script通过javascript加载wp_editor。但是,当我尝试克隆/附加表单字段时,它不会加载wp_editor而是加载简单的textarea。

我相信DOM不会加载wp_editor() js函数。那么有人能告诉我如何为克隆字段加载wp_editor吗?

的jQuery

// Just to cross check. This is loading wp_editor on page load
jQuery('.cn-wp-editor').wp_editor();

// wp_localization
var title = cn_fields.title;
var teditor = cn_fields.editor;

// adding incremental id
var i = 1;

// clone fields
$('#add_item').on('click', function () {
    i++;
    $('#fieldgroup').append('<div class="formgroup"><div class="card-meta-box"><label for="card_title" class="card-field-label">Item Title</label><input type="text" name="' + title + '[]" id="' + title + i +'"></div><div class="card-meta-box"><textarea name="' + teditor + '[]" id="' + teditor + i +'" class="cn-wp-editor"></textarea></div><button class="remove">x</button></div><!-- formgroup -->');
    return false; //prevent form submission
});

// remove fields
$('#fieldgroup').on('click', '.remove', function () {
    $(this).parent().remove();
    return false; //prevent form submission
    i--;
});

1 个答案:

答案 0 :(得分:2)

  

每当您复制/复制时,您需要重新初始化wp_editor()   场。您的代码不能作为复制/创建的字段   在页面加载的DOM中,wp_editor()没有附加到那些   新领域。

检查此代码:

$('#add_item').on('click', function () {
    i++;
    $('#fieldgroup').append('<div class="formgroup"><div class="card-meta-box"><label for="card_title" class="card-field-label">Item Title</label><input type="text" name="' + title + '[]" id="' + title + i + '"></div><div class="card-meta-box"><textarea name="' + teditor + '[]" id="' + teditor + i + '" class="cn-wp-editor"></textarea></div><button class="remove">x</button></div><!-- formgroup -->');
    $('#' + title + i).wp_editor(); //<------ add this line
    return false; //prevent form submission
});

希望这有帮助!