如何使用JS/jQuery检查CK Editor是否有输入?

How can I check whether CK Editor has input using JS/jQuery?

在我的应用程序中,有一个文本区域,我为此使用了 CK 编辑器。现在我想检查用户是否输入了一些东西(空格除外)。有一些方法可以做到这一点。但对我没有任何帮助。当我使用它时它会抛出一个未定义的错误。

文本区域

<div class="form-group {{ $errors->has('template_content') ? 'has-error' : '' }}">
    {{ Form::label('template_content', 'Template Content') }}
    {{ Form::textarea('template_content', null, array('class' => 'form-control', 'id' => 'editor1', 
    'name' => 'editor1', 'required', 'data-error="Template content is required"', 'id' => 
   'templateContent')) }}
    {!! $errors->first('template_content', '<span class="help-block">:message</span>') !!}
    <div class="help-block with-errors"></div>
</div>

JS函数

var a = CKEDITOR.instances["editor1"].getData();

    if (a == '') {
        //code to execute
    } else {
        //code to execute
    }

错误

Uncaught TypeError: Cannot read property 'getData' of undefined

第一个

    {{ Form::textarea('template_content', null, array('class' => 'form-control', 'id' => 'editor1', 
    'name' => 'editor1', 'required', 'data-error="Template content is required"', 'id' => 
   'templateContent')) }}

这里你有两个id,你的textarea id是templateContent。删除 'id' => 'templateContent'

其次,这不是守望者!它不会实时更新你。你需要以某种方式触发它,例如

<button id="test">get data</button>

$(document).ready(function() {
  $("#test").click(function() {
    var a = CKEDITOR.instances["editor1"].getData();

    console.log(a);
  });
});

单击按钮并检查控制台。