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

标签 javascript jquery laravel ckeditor typeerror

在我的应用程序中,有一个文本区域,我使用了 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' => '模板内容'

其次,这不是一个观察者!,它不会实时更新您的信息。例如,您需要以某种方式触发它

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

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

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

单击按钮并检查控制台。

关于javascript - 如何使用 JS/jQuery 检查 CK Editor 是否有输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60223395/

相关文章:

javascript - 损坏的 v8 日期解析器有什么解决方法吗?

javascript - 更改 Google Places 自动完成结果框的位置

javascript - Google map 中的用户位置

javascript - 无法使用 promise.all() 获得 promise 结果

javascript - 如何使用 jquery 改变动画速度?

php - Laravel:是否存在等价于 var_export($var, True) 的助手?

laravel - 如何使用添加的列作为查询正确更新数据透视表?

php - 在 Laravel 中使用 Guzzle 发送 POST 请求

javascript - 在单独的页面上显示结果

javascript - jquery访问所有具有id属性的元素