我使用以下代码在单击带有data-attachment_id
属性此属性保存框架打开时要选择的附件的id:
jQuery(document).ready(function($){
$( \'#gallery_images_container\' ).on( \'click\', \'a.edit\', function( event ) {
var $el = $( this );
var selected = $( this ).attr( \'data-attachment_id\' );
event.preventDefault();
// If the media frame already exists, reopen it.
if ( gallery_items_frame ) {
// Select the attachment when the frame opens
gallery_items_frame.on( \'open\', function() {
var selection = gallery_items_frame.state().get( \'selection\' );
if ( selected ) {
selection.add( wp.media.attachment( selected ) );
}
});
// Open the modal.
gallery_items_frame.open();
return;
}
// Create the media frame.
gallery_items_frame = wp.media.frames.gallery_items = wp.media({
// Set the title of the modal.
title: $el.data( \'choose\' ),
button: {
text: $el.data( \'update\' )
},
states: [
new wp.media.controller.Library({
title: $el.data( \'choose\' ),
filterable: \'all\',
multiple: true
})
]
});
// Select the attachment when the frame opens
gallery_items_frame.on( \'open\', function() {
var selection = gallery_items_frame.state().get( \'selection\' );
if ( selected ) {
selection.add( wp.media.attachment( selected ) );
}
});
// Open the modal.
gallery_items_frame.open();
});
});
当我第一次单击链接时,框架将打开,并选择相关附件。但是,如果关闭框架并再次单击链接,框架将再次打开,但未选择任何附件。
关于我可能做错了什么,有什么见解吗?
提前感谢