【发布时间】:2015-02-23 04:13:06
【问题描述】:
我在一个在 wordpress 中打开媒体上传窗口的 jQuery 函数上遇到了范围问题(我认为)。
我有一个在多个按钮上使用的功能,但是在返回结果时我需要参考具体的按钮。看看我的代码:
var file_frame;
jQuery('.bjqs_upload_button').live('click', function( event ){
event.preventDefault();
current_id = $(this).attr('id');
[...]
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
// Set the thumbnail on page to the image just selected
$("#". current_id).parent().find('img').attr("src", attachment.url);
});
...
});
在上面的代码中,我使用代码收集了当前按钮的id:
current_id = $(this).attr('id');
然后我试图在后面的 file_frame.on() 函数中引用这个。
$("#". current_id).parent().find('img').attr("src", attachment.url);
对此运行警报时,我得到一个“未防御者”。
我试图像这样将变量传递给函数
file_frame.on( 'select', function(current_id) {
但问题仍然存在。
如果可能的话,希望能得到一些帮助。
谢谢
【问题讨论】:
标签: jquery wordpress scope media uploader