JQuery div选择器没有按预期遍历

时间:2016-05-16 15:51:34

标签: jquery html ajax

我正在尝试通过ajax将一些内容加载到之前使用另一个ajax函数加载的div中,并且我很难将数据放入div中。要么,它们都装载了相同的内容,或者根本没有。我确信这是一件非常简单的事情,我很遗憾 - 任何帮助都会非常感激。

Ajax功能:

$(document).on('click', '.btn.btn-clearview.loadSubresults', function () {
$.ajax({
   type: "POST",
   url: "loadData.php",
   data: $(this.form).serialize(), // serializes the form's elements.
   dataType: 'json',
   success: function(data)
   {
       console.log(data);
       $(this).closest('.wrapper').children('.resultExpander').html(data); // Should be doing something but nothing is happening (OPTION 1)
       //$('.resultExpander').html(data); // Load the response from the PHP into the HTML (OPTION 2)
   },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
 });

这一切如何结合在一起:

用户加载主要结果页面 - >单击搜索按钮并通过ajax加载结果。这个结果有一个带有“结果”类的div,并且这个div有多个实例。

根据我的理解,使用遍历规则(https://learn.jquery.com/using-jquery-core/traversing/)我可以定位所需的div,即使在整个过程中使用相同的类名。

Firefox上控制台的错误消息/信息

none - 如果我使用选项1

,则不加载

感谢。

1 个答案:

答案 0 :(得分:1)

$(document).on('click', '.btn.btn-clearview.loadSubresults', function () {
  $_this = $(this);
  $.ajax({
    type: "POST",
    url: "loadData.php",
    data: $(this.form).serialize(), // serializes the form's elements.
    dataType: 'json',
    success: function(data)
    {
      $_this.closest('.wrapper').children('.resultExpander').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
      alert(xhr.status);
      alert(thrownError);
    }
  });
});

在ajax之前将$(this)分配给变量然后通过内部成功调用

相关问题