javascript CKEDITOR 在第一次提交时不会通过 ajax 提交数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24398225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:43:00  来源:igfitidea点击:

CKEDITOR doesn't submit data via ajax on first submit

javascriptphpjqueryajaxforms

提问by davidxd33

My forms are not sending data to the server on the first submit when using CKEDITOR. If I click it once, it sends empty fields without my input. However, if I submit it a second time, it sends the inputted data to the server. So you need to submit it twice for data to be passed to the server.

使用 CKEDITOR 时,我的表单没有在第一次提交时将数据发送到服务器。如果我单击一次,它会在没有我输入的情况下发送空字段。但是,如果我第二次提交它,它会将输入的数据发送到服务器。所以你需要提交两次才能将数据传递到服务器。

I have CKEDITOR bundled with the BBCODE plugin.

我将 CKEDITOR 与 BBCODE 插件捆绑在一起。

jQuery Ajax

jQuery Ajax

$('form#ajax').on('submit', function(){

    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value){
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    console.log(data.message); // Outputs on second submit

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){

            //

        }
    });

    return false;
});

Form

形式

{{ Form::open(array('action' => 'AppsController@sendApp', 'class' => 'app', 'id' => 'ajax')) }}

    <div class="form-container">

        {{ Form::label('message', 'Application', array('style' => 'padding-top: 5px')) }}

            <textarea name="message" cols="50" rows="6" class="form-control" id="eitor" style="padding-top:5px;"></textarea>


    </div>

        {{ Form::submit('Send Application', array('class' => 'btn btn-core btn-block submit', 'style' => 'margin-top: 5px')) }}

{{ Form::close() }}

Sorry if the form syntax looks alien to you, it's Laravel Blade.

抱歉,如果表单语法对您来说很陌生,那就是 Laravel Blade。

Recap

回顾

On first submit, the data sent to the server is empty. On the second submit, it is not.

第一次提交时,发送到服务器的数据是空的。在第二次提交时,它不是。

回答by Sudhir Bastakoti

try updating the CKEditor related fields, before performing Ajax Submit, like:

在执行 Ajax 提交之前尝试更新 CKEditor 相关字段,例如:

$('form#ajax').on('submit', function(){
    for ( instance in CKEDITOR.instances ) {
        CKEDITOR.instances[instance].updateElement();
    }
    //rest of your code