2017-04-25 68 views
0

我需要更新tinymce窗口中提交的變量。如何在提交Tinymce內容之前等待ajax數據

這裏TinyMCE的代碼:

tinymce.PluginManager.add('tc_button',function(editor,url){ 
    editor.addButton('tc_button',{ 
    title: 'Images', 

    onclick: function(){ 
    editor.windowManager.open({ 
    title: 'Images', 
    buttons: [{text: 'Add Shortcode', onclick: 'submit'}, {text: 'Cancel', onclick: 'close'}], 

    body: [ 

    {type: 'container', 
    label : 'Image URL', 
    items: [ 
    {type: 'textbox', id: 'image1', name : 'image1', style:'width:270px'}, 
    {type: 'listbox', id: 'effect1', name: 'effect1', style:'margin-left:10px'}, 
    ]} 

    ], 

    onsubmit: function(e) { 


    jQuery.post(ajax_url, 
    { 
    action: 'save_image', 
    image: imageData, 
    } 
    ), 

    function (data){ 
    var jsonData = tryParseJSON(data); 
    if (jsonData !== false) { 
    var new_url = jsonData.attachment_url; 
    jQuery('#image1').attr('src', new_url);  
    } 
    }; 



    editor.insertContent('[shortcode image1="' + e.data.image1 + '"]'); 


    } 

    }); 

    } 
    }); 
    }); 

我需要之前editor.insertContent運行jQuery('#image1').attr('src', new_url) Ajax調用內內編輯添加。我如何設置它,以便等待ajax調用結束並且#image1源用ajax結果值更新?

在此先感謝。

+0

你爲什麼不把'editor.insertContent(...)'回調裏面? –

+0

我試過了,但'editor.insertContent'不起作用。沒有添加短代碼。 –

回答

0

您需要致電editor.insertContent('[shortcode image1 =''+ e.data.image1 +'「]');內部回調函數jQuery('#image1')。attr('src',new_url);

tinymce.PluginManager.add('tc_button', function(editor, url) { 
    editor.addButton('tc_button', { 
     title: 'Images', 

     onclick: function() { 
      editor.windowManager.open({ 
       title: 'Images', 
       buttons: [{ 
        text: 'Add Shortcode', 
        onclick: 'submit' 
       }, { 
        text: 'Cancel', 
        onclick: 'close' 
       }], 

       body: [ 

        { 
         type: 'container', 
         label: 'Image URL', 
         items: [{ 
           type: 'textbox', 
           id: 'image1', 
           name: 'image1', 
           style: 'width:270px' 
          }, 
          { 
           type: 'listbox', 
           id: 'effect1', 
           name: 'effect1', 
           style: 'margin-left:10px' 
          }, 
         ] 
        } 

       ], 

       onsubmit: function(e) { 


        jQuery.post(ajax_url, { 
          action: 'save_image', 
          image: imageData, 
         }), 

         function(data) { 
          var jsonData = tryParseJSON(data); 
          if (jsonData !== false) { 
           var new_url = jsonData.attachment_url; 
           jQuery('#image1').attr('src', new_url); 
          } 
          editor.insertContent('[shortcode image1="' + e.data.image1 + '"]'); 
         }; 
       } 

      }); 

     } 
    }); 
}); 
+0

我做到了,但在提交按鈕上,tinymce窗口關閉,ajax調用處理良好,並且在wp tinymce編輯器中沒有添加短代碼。 –

相關問題