gpt4 book ai didi

javascript - jQuery 克隆表单字段组将 wp_editor() 函数添加到文本区域

转载 作者:行者123 更新时间:2023-11-30 15:17:07 26 4
gpt4 key购买 nike

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

所以 here我找到了一个 script它通过 javascript 加载 wp_editor。但是,当我尝试克隆/附加表单字段时,它不会加载 wp_editor,而是加载简单的文本区域。

我相信 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--;
});

最佳答案

You need to reinitialized wp_editor() whenever you duplicate/copy the field. Your code is not working as the copied/created field in not in DOM on page load so wp_editor() was not getting attached to those new field(s).

检查这段代码:

$('#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
});

希望这对您有所帮助!

关于javascript - jQuery 克隆表单字段组将 wp_editor() 函数添加到文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com