CKEDITOR在首次提交时不通过ajax提交数据

时间:2014-06-25 00:03:04

标签: javascript php jquery ajax forms

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

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

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::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() }}

很抱歉,如果表单语法看起来与您不同,那就是Laravel Blade。

小结

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

1 个答案:

答案 0 :(得分:30)

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

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