jQuery 克隆表单字段组添加 wp_editor() 函数到文本区域
jQuery clone form field group add wp_editor() function to the textarea
在 CPT metabox 中,我使用的是 wp_editor()
。第一个是通过 php 函数加载。但是,当我通过 jQuery 克隆表单字段时,它不会添加 wp_editor 而是简单的文本区域。
所以 here I found a script 通过 javascript 加载 wp_editor。但是,当我尝试 clone/append 表单字段时,它不会加载 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
});
希望对您有所帮助!
在 CPT metabox 中,我使用的是 wp_editor()
。第一个是通过 php 函数加载。但是,当我通过 jQuery 克隆表单字段时,它不会添加 wp_editor 而是简单的文本区域。
所以 here I found a script 通过 javascript 加载 wp_editor。但是,当我尝试 clone/append 表单字段时,它不会加载 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 sowp_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
});
希望对您有所帮助!