如何在jQuery UI对话框中返回值?

如何在jQuery UI对话框中返回值?,jquery,tinymce,jquery-ui-dialog,Jquery,Tinymce,Jquery Ui Dialog,我需要返回在jQueryUI对话框中选择的som值 目前,我只是设置如下值: jQuery('#fileBrowser input.addImage').live("click", function() { // 'file' is set when selected in file browser imageUrlInputBox.val(file); // Add relative image url to text field jQuery("#fileBrowser

我需要返回在jQueryUI对话框中选择的som值

目前,我只是设置如下值:

jQuery('#fileBrowser input.addImage').live("click", function() {
  // 'file' is set when selected in file browser
  imageUrlInputBox.val(file);      // Add relative image url to text field
  jQuery("#fileBrowser").dialog("close");
});
然而,我现在面临的问题是,我通过TinyMCE中的自定义按钮打开对话框。所以我需要另一种插入图像的方法。这就是我想到的:

// This is the function valled when clicking the tinyMCE button
function openImageManager(ed) {        
  //tinymce is a global variable.      
  tinymce = ed; 
  jQuery("#fileBrowser").dialog("open");
}
该函数接收从tinyMCE插件传递的“ed”变量。以下是这方面的脚本:

(function() {

    tinymce.create('tinymce.plugins.wp_filebrowser_plugin', {

        init : function(ed, url){
            ed.addButton('wp_filebrowser_plugin', {
                title : 'Insert image',
                onclick : function() {
                  openImageManager(ed) 
                },
                image: url + "/img/wand.png"
            });
        },

        getInfo : function() {
            return {
                longname : 'WP Filebrowser TinyMCE plugin',
            };
        }
    });

    tinymce.PluginManager.add('wp_filebrowser_plugin', tinymce.plugins.wp_filebrowser_plugin);
})();
现在,单击“插入”按钮时,我可以执行以下代码将数据插入文本编辑器:

jQuery('#fileBrowser input.addImage').live("click", function() {
  var img_html = '<img class="' + css_class + '" src="' + file_url + '" title="' + alt + '" alt="" />';
  tinymce.execCommand('mceInsertContent', false, img_html);  
});
jQuery('#fileBrowser input.addImage').live(“单击”,函数(){
var img_html='';
execCommand('mceInsertContent',false,img_html);
});
解决方案

感谢T.J.Crowder,我找到了答案。代码已更新以反映这一点。

您不能执行此操作:

function openImageManager() {
  img_html = jQuery("#fileBrowser").dialog("open"); // I need some sort of callback here
  return img_html;
}
…因为您的对话框与用户的交互需要与
openImageManager
调用异步,所以在等待UI事件(如用户正在做某事)发生时,无法将
openImageManager
函数“挂起”


您需要做的是显示对话框,然后在别处处理关闭对话框并将图像粘贴到TinyMCE中(例如,通过
execCommand
发送
mceImage
命令)。您不能将其作为
openImageManager
函数的返回值。

啊,谢谢您的评论。它帮助我找到了解决办法。查看我的更新代码。@Steven:太好了,很高兴能帮上忙。