Javascript 如何ajax提交来自CKEditor的表单textarea输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3256510/
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-08-23 03:54:15  来源:igfitidea点击:

How to ajax-submit a form textarea input from CKEditor?

javascriptjqueryckeditorformsjquery-forms-plugin

提问by fabien7474

I am using CKEditor, jQuery and jQuery form pluginand I would like to submit contents of the CkEditor form via an Ajax query. Here is my code:

我正在使用 CKEditor、jQuery 和jQuery 表单插件,我想通过 Ajax 查询提交 CkEditor 表单的内容。这是我的代码:

<form id="article-form" name="article-form" method="post" action="/myproject/save">
  <textarea name="bodyText" style="visibility: hidden; display: none;"></textarea>
  <script type="text/javascript">
    CKEDITOR.replace('bodyText');
  </script>

  <a onClick="$("#article-form").ajaxSubmit();">Submit</a>

</form>

Unfortunately, it seems that the Ajax request does not pass the bodyTextparameter;

不幸的是,Ajax 请求似乎没有传递bodyText参数;

What did I do wrong or how can I achieve what I need?

我做错了什么或者我怎样才能达到我需要的?

Thank you.

谢谢你。

回答by Gabriele Petrioli

you need to first call the following, to make the CKEDITORs update their related fields..

您需要首先调用以下内容,以使 CKEDITOR 更新其相关字段..

for ( instance in CKEDITOR.instances )
    CKEDITOR.instances[instance].updateElement();

so

所以

HTML

HTML

<a onClick="CKupdate();$('#article-form').ajaxSubmit();">Submit</a>

and javascript

和 javascript

function CKupdate(){
    for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();
}

回答by Pepa Chmel

This works for me best: beforeSerialize callback

这对我最有效:beforeSerialize 回调

$('form#description').ajaxForm({
    beforeSerialize:function($Form, options){
        /* Before serialize */
        for ( instance in CKEDITOR.instances ) {
            CKEDITOR.instances[instance].updateElement();
        }
        return true; 
    },
    // other options
});

回答by Crewone

If you use the jQuery form plugin, you can use the beforeSubmit option for a more elegant solution:

如果您使用jQuery 表单插件,您可以使用 beforeSubmit 选项以获得更优雅的解决方案:

$("#form").ajaxForm({
    beforeSubmit:  function()
{
        /* Before submit */
    for ( instance in CKEDITOR.instances )
    {
        CKEDITOR.instances[instance].updateElement();
    }
},

  // ... other options
});

回答by Milan Saha

In my case the following helped me,i just use these two lines before seializing the form.

在我的情况下,以下内容对我有帮助,我只是在密封表单之前使用这两行。

  for ( instance in CKEDITOR.instances )
       CKEDITOR.instances[instance].updateElement();

  var data = $('#myForm').serializeArray();

回答by EugeneEunice

I tried something like this:

我试过这样的事情:

First I had to put an id = "#myForm" on @Html.BeginForm afterwards, I put these in my scripts part where in I use the script:

首先,我必须在@Html.BeginForm 上放置一个 id = "#myForm" 之后,我将这些放在我使用脚本的脚本部分中:

<script type="text/javascript">
    $(document).ready(function CKupdate() {
        $('#myForm').ajaxForm(function () {
            for (instance in CKEDITOR.instances) {
                CKEDITOR.instances[instance].updateElement();
            }
        });       
    });
</script>

and then I did something like this =] for my submit button and it works fine for me, no more pressing the Submit twice =]

然后我为我的提交按钮做了这样的事情 =] 对我来说效果很好,不再按两次提交 =]

<button type="submit" id="submitButton" onclick="CKupdate();$('#myForm').ajaxSubmit();">Submit</button>

回答by Omu

I just did it like this:

我只是这样做:

$('#MyTextArea').closest('form').submit(CKupdate);

        function CKupdate() {
            for (instance in CKEDITOR.instances)
                CKEDITOR.instances[instance].updateElement();
            return true;
        }