2010-11-28 62 views
2

我需要返回我在jQuery UI对话框中选择的som值。如何在jQuery UI对话框中返回值?

目前我只是设定值是这样的:

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插件通过“编”变量。下面是该脚本:

(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); 
}); 

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

回答

2

不能做到这一点:

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

...因为你的对话框与用户的交互必须异步openImageManager电话,有没有办法把openImageManager在等待UI事件(如用户做某事)时发生的功能“保持”。

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

+0

啊,谢谢你的评论。它帮助我找到解决方案。查看我更新的代码。 – Steven 2010-11-28 14:10:30