[英]TinyMCE validation gives error: Cannot call method 'getContent' of undefined
我有一个文本区域,其中的mce很小,我这样加载它:
$(document).ready(function () {
tinyMCE.init({
mode: "textareas",
...
此文本区域为表格。 我将表单提交按钮绑定到:
$('#btnSubmit').click(function() {
tinymce.triggerSave();
var editorContent = tinyMCE.get('Description').getContent();
if (editorContent == '' || editorContent == null)
{
$(tinymce.activeEditor.getBody()).css("background-color", '#ffeeee');
$(tinymce.activeEditor.getBody().parentNode).css("background-color", '#ffeeee');
$(tinymce.activeEditor.getBody().parentNode).css("border", '1px solid #ff0000');
}
});
在我的实体类中,我具有Required
属性。
我的目标是在模型无效时使tinyMCE背景变为red
。 但是我从题题中得到了错误。
有什么帮助吗?
因此,验证有效。 如果我删除了textarea空支票,并保留了更改的颜色,它将更改。 但是问题是当文本区域中有东西时,我单击“提交区域”,然后先变成红色然后提交。
如果验证失败,也许有些功能可以使我做什么?
在我看来,这就像是未定义的对象错误-代码无法解析此行tinyMCE.get('Description').getContent();
。
您似乎有时会在使用activeEditor
之间混合使用, activeEditor
有时则不使用,因此我对代码进行了标准化,因此您始终依靠activeEditor
这意味着我已删除了触发错误的行。 您似乎也可以在使用tinymce
和tinyMCE
之间切换,这可能不会引起问题,但最好避免...因此我也对此进行了标准化。
没有看到更多的方式来设置其余的代码和标记,但是很难准确地知道发生了什么。 我的零钱能解决问题吗?
$('#btnSubmit').click(function() {
tinyMCE.triggerSave();
var editorContent = tinyMCE.activeEditor.getContent();
if (editorContent == '' || editorContent == null)
{
$(tinyMCE.activeEditor.getBody())
.css("background-color", '#ffeeee')
.parent()
.css({
"background-color": '#ffeeee',
"border": '1px solid #ff0000'
});
}
});
如果您无法控制TinyMCE的初始化方法,则可以遵循此解决方案。
jQuery(document).ready(function($) {
function myCustomSetContent( id, content ) {
// Check if TinyMCE is defined or not.
if( typeof tinymce != "undefined" ) {
var editor = tinymce.get( id );
// Check if TinyMCE is initialized properly or not.
if( editor && editor instanceof tinymce.Editor ) {
editor.setContent( text );
editor.save( { no_events: true } );
} else {
// Fallback
// If TinyMCE is not initialized then directly set the value in textarea.
//TinyMCE will take up this value when it gets initialized.
jQuery( '#'+id ).val( text );
}
return true;
}
return false;
}
function myCustomGetContent( id ) {
// Check if TinyMCE is defined or not.
if( typeof tinymce != "undefined" ) {
var editor = tinymce.get( id );
// Check if TinyMCE is initialized properly or not.
if( editor && editor instanceof tinymce.Editor ) {
return editor.getContent();
} else {
// Fallback
// If TinyMCE is not initialized then directly set the value in textarea.
// TinyMCE will take up this value when it gets initialized.
return jQuery( '#'+id ).val();
}
}
return '';
}
$(".class-to-update-content").on("click", function(e) {
myCustomSetContent( "tinymce-editor-id", "New Content in Editor" );
});
$(".class-to-get-content").on("click", function(e) {
$("div.class-to-display-content").html( myCustomGetContent( "tinymce-editor-id" ) );
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.