在类的下一个实例中显示ajax响应

jquery display ajax response in next instance of class

本文关键字:显示 ajax 响应 实例 下一个      更新时间:2023-09-26

我有一个使用while循环显示结果的页面。如果用户单击evernote按钮,则使用ajax通过电子邮件发送来自相邻结果的信息。我试图得到ajax响应显示div的实例与类sent_to_evernote,这是相邻的点击按钮。

<td>
   <input type="button" class="evernote_todo" value="Evernote"  />
   <div class="sent_to_evernote"></div>
</td>

我知道ajax成功返回正确的响应,但我只是不能得到它显示。

success: function(response){
    if(response == 'sent'){
        $(this).parent('td').find('div.sent_to_evernote').html(response);
    }
}//end success

this是Evernote按钮

您确实没有显示太多的代码上下文,但this不是$.ajax回调中的dom元素。

你可以做几件事

一个是在事件处理程序中存储this的变量引用,或者将this传递给$.ajaxcontext选项

$(selector).on('some_event', function(){
    var self = this; // one way - store reference outside of callback
    $.ajax({
       url: '....',
       context: this, // alternate way - pass in `this` as callback context
       success:function(response){
          if(response == 'sent'){
             // depending on approach use $(this) or $(self)  
             $(this).parent('td').find('div.sent_to_evernote').html(response);
          }
       }
    })
})