我正在研究主题开发,正在为媒体上传程序编写一些代码,可以用来添加图片。文件框架正在打开(我已调用wp_enqueue_media();
如果是这样的话),但选择图片后不会发生任何事情。
我想我错过了一些简单的东西,坦率地说,我正在疯狂地寻找它。谁能帮帮我吗?
jQuery(document).ready(function($){
var mediaUploader;
$(\'#upload-button\').on(\'click\', function(e){
e.preventDefault();
if(mediaUploader){
mediaUploader.open();
return;
}
mediaUploader = wp.media.frames.file_frame = wp.media({
title: \'Choose a profile picture\',
button: {
text: \'Choose Picture\'
},
multiple: false
});
//This alert fires.
alert("First message.");
$(mediaUploader).on(\'select\', function(){
//This alert does not fire.
alert("Second message.");
});
mediaUploader.open();
});
});
最合适的回答,由SO网友:sadq3377 整理而成
Embarrassing.
$(mediaUploader).on(\'select\', function(){
Should be:
mediaUploader.on(\'select\', function(){
In case anybody stumbles upon this, $({item}) looks for the item in the DOM, not the variable of the same name in the previous code. Since the select event isn\'t firing in the DOM, the callback function isn\'t going to fire either.