多个媒体上传器输出到输入

时间:2015-06-26 作者:Ahmed Saad

我试图选择多个图像,然后将ID(逗号分隔)插入文本字段。

这是我的代码,但它只选择第一个图像ID:

jQuery(document).ready(function($){
var custom_uploader;
$(\'#media-button\').click(function(e) {
    e.preventDefault();
    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
        custom_uploader.open();
        return;
    }
    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
        title: \'Choose Image\',
        button: {
            text: \'Choose Image\'
        },
        multiple: true
    });
    custom_uploader.on(\'select\', function() {
        var selection = custom_uploader.state().get(\'selection\');
        selection.map( function( attachment ) {
        $("#media-input").val(attachment.id);
        });
    });
    custom_uploader.open();
});
});
应更改的零件是以custom_uploader.on(\'select\'

1 个回复
最合适的回答,由SO网友:bonger 整理而成

根据注释,尝试收集数组中的ID,然后设置一次文本字段:

            custom_uploader.on(\'select\', function() {
                var selection = custom_uploader.state().get(\'selection\');
                var ids = selection.map( function (attachment) {
                    return attachment.id;
                });
                $("#media-input").val(ids.join(\',\'));
            });

结束