WP Media Library / Uploader不会返回动态HTML / DOM

时间:2013-02-14 11:43:31

标签: jquery wordpress media uploader wordpress-3.5

样本来自Display Media Uploader in Own Plugin on Wordpress 3.5

<script type="text/javascript">
var file_frame;

jQuery('.button-secondary').live('click', function( event ){

    event.preventDefault();

    if ( file_frame ) {
        file_frame.open();
        return;
    }

    file_frame = wp.media.frames.file_frame = wp.media(
        {
            title: 'Select File',
            button: {
                text: jQuery( this ).data( 'uploader_button_text' )
            },
            multiple: false
        }
    );

    file_frame.on('select', function() {
        attachment = file_frame.state().get('selection').first().toJSON();
        jQuery('#IMGsrc').val(attachment.url);
    });

    file_frame.open();
});

问题是关于

file_frame.on('select', function() {...});

不要返回DYNAMIC html。我尝试过这样的代码:

jQuery(document).on('select', file_frame, function() {...});
jQuery(document).on('select', file_frame.el, function() {...});

但没有工作......

2 个答案:

答案 0 :(得分:0)

出于兼容性原因,即使在新的上传程序中也会调用window.send_to_editor(html),但我不认为这样做时会有效。您可以使用所选对象的属性,并创建链接/图像html。

答案 1 :(得分:0)

我与我认为类似的问题进行了斗争。部分问题是上传后,第一个附件对象为空并导致错误。

以下是我在我的网站上输出HTML的代码示例:

//选择图像时,将ID放在隐藏的自定义字段中并显示缩略图。    file_frame.on('select',function(){

var selection = file_frame.state().get('selection');

// Show Thumbs
var attachment_thumbs = selection.map( function( attachment ) {
  attachment = attachment.toJSON();
  if( attachment.id != '' ) { return '<img src="' + attachment.sizes.thumbnail.url + '" id="id-' + attachment.id + '" />'; }
}).join(' ');
$('#images-feedback').show();
$('#thumbs').html(attachment_thumbs);

});

希望这有帮助!