tinymce在document.ready中设置vaue

时间:2015-11-15 10:39:11

标签: jquery ajax tinymce

我使用tinymce4.2.6,在母版页中我放置了这段代码

   <script type="text/javascript">


      tinymce.init({
        selector: "textarea",
        directionality: 'rtl',
        plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste",
        "textcolor"


    ],
        toolbar: "insertfile undo redo  | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent  | fontselect |  fontsizeselect |  forecolor backcolor"


    });

   </script>

我的问题是我无法在编辑器中加载数据。我使用setcontent方法,但它不起作用。我已将此代码放在我的页面中:

 <script type="text/javascript">
$(document).ready(function () {
 load_data();

 function loaddata() {

        $.ajax({
            type: "POST",
            url: "../WebService.asmx/loaddata",
            data: "{'id':'" + "1" + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {

                var myData = data.d;


                tinyMCE.get("textareaId").setContent(myData);

            }
        });
    }
});  
 </script>

当我发出警报(MyData)时;这是正确的信息,但textarea是空的

1 个答案:

答案 0 :(得分:0)

您必须等待编辑器加载才能将内容插入其中。 document.ready在这种情况下已经胜利了。使用:

  tinymce.init({
    selector: "textarea",
    directionality: 'rtl',
    plugins: [
    "advlist autolink lists link image charmap print preview anchor",
    "searchreplace visualblocks code fullscreen",
    "insertdatetime media table contextmenu paste",
    "textcolor"
],
   setup : function(ed) {
      ed.on('init', function(evt) {

      // get your content
      // and insert it into the editor
      ed.setContent(myData);


      });
    },
    toolbar: "insertfile undo redo  | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent  | fontselect |  fontsizeselect |  forecolor backcolor"


});