如何在jQuery UI Dialog中返回值?

时间:2010-11-28 13:34:31

标签: jquery tinymce jquery-ui-dialog

我需要返回我在jQuery UI对话框中选择的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);  
});


感谢T.J.克劳德,我找到了答案。代码已更新以反映此情况。

1 个答案:

答案 0 :(得分:2)

不能这样做:

function openImageManager() {
  img_html = jQuery("#fileBrowser").dialog("open"); // I need some sort of callback here
  return img_html;
}

...因为您的对话框与用户的交互需要异步openImageManager调用,所以无法将openImageManager函数置于保持状态“在等待UI事件(比如用户做某事)时发生。

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