Get CKEditor value using Jquery in django framework

北战南征 提交于 2021-01-05 06:39:46

问题


I'm using CkEditor for Django and I have a form with 3 fields, a product code, a subject and a body. The first two fields are normal fields and the body field is a CKEditor field that is being rendered on the template from forms.py. In order to do an ajax call on this data, I need to use Jquery to get the CkEditor content. How to do this? This is my code so far.

My template:

form action="." method="post">    
    {{form2.as_p}}
    {% csrf_token %}
    <br><button name="form_submit"  id="email_submit" type="submit" class="btn btn-primary" value="send_email">Submit</button>
</form>

My forms:

class CustomersWhoBoughtForm(forms.Form):
    product_code = forms.CharField(required=True,  max_length=1000, label='Product Code')
    subject = forms.CharField(required=True, max_length=1000,  label='Email Subject')
    body = forms.CharField(widget=CKEditorUploadingWidget(config_name='default')) #trying to get value of this field for Ajax Call

I tried this:

var product_id= $('#id_product_code').val();   //This works
var subject = $('#id_subject').val();          //Also works
var body = $('#id_body').val();                //Does not work, neither does ck_id_body

回答1:


This question nothing to do with django or python. It is pure about how to get the content from CkEditor and if you search there will be tons of answers on this topic.

Here is how you can get the content of the editor:

var value = CKEDITOR.instances['id_body'].getData()



回答2:


I also had the same issue with django-ckeditor,What I tried is

<script type="text/javascript">
     for (var instance in CKEDITOR.instances)
            CKEDITOR.instances[instance].updateElement();


var temp = CKEDITOR.instances[instance].getData();

it works fine,the variable named temp holds the value.



来源:https://stackoverflow.com/questions/42938965/get-ckeditor-value-using-jquery-in-django-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!