【问题标题】:jQuery and wordpress - multiple button function scopejQuery 和 wordpress - 多按钮功能范围
【发布时间】: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


    【解决方案1】:

    所以我最终在这个上使用了全局变量(我最初试图避免这样做。

    设置变量

    window.current_id = $(this).attr('id');
    

    并在函数中这样调用它:

    $("#" + window.current_id).parent().find('img').attr("src", attachment.url);
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多