I´m trying to get typed text on ckeditor (textarea), but I have some trouble:

Here is my code:

<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    $('#editor').ckeditor();


    var editor = $('#editor').ckeditorGet(); 

    var data = $('#editor').val(); 
    window.alert(data);
    window.alert(CKEDITOR.instances['editor'].getData());

});
</script>

    <body>


<form method="post">
    <textarea  name="editor" id="editor"></textarea>
<input type="submit" value="Submit">
</form>

The results on two alerts are empty. What i´m doing wrong?

有帮助吗?

解决方案

That's because you are calling the alerts when the page loads. At that time, there is nothing yet on the textarea.

Bind the event to something that will happen after the textbox has something to show, for example, when you click the submit button:

$(document).ready(function(){
    $('#editor').ckeditor();
    $('input[type=submit]').on('click', function() {
        window.alert($('#editor').val());
    });
});

Also, you may want to bind the click event to the document instead, so it will happen even if you add new submits programatically. For that to happen, bind the event like this:

$(document).on('click', 'input[type=submit]', function() {
    window.alert($('#editor').val());
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top