如何选择和触发$ .ajax的父元素?

如何选择$ .ajax的父元素,或触发$ .ajax元素的父元素?我需要某种参考,从而将结果数据应用于它:

var a = $a.val();
var b = $b.val();

 $.ajax({
      type: "POST",
      url: "/Controller/Action",
      data: { a: a, b: b },
      success: function (data) {

          var values = data.values,
              $elements = $();
              for (i = 0; i < 142; i++) {
                  $elements = $elements.add($("<div class='single'>").css('height', values[i]).css('margin-top', 26 - valuesG[i]));
              }
                //Here it should reference $(this).parent().parent().. something
               //and not :last, because there are many .second elems... 
              //not only :last is being changed?

              $elements.appendTo($(".second:last"));
              $(".second:last").children(".single").addClass("ui-selected");
      },
      traditional: true
});   

ajax success函数中的$(this).parent()返回了jQuery()。

Use the context option:

$.ajax({
    url: "test.html",
    context: document.body,
    success: function(){
      $(this).addClass("done");
   }
});

jQuery Reference

because by default ajax call is asynch and so when you do $(this) inside success the this reference to ajax api and so $(this).parent() reference to jQuery().

to avoid this save the element reference in a variable before ajax call starts to use inside success.

target_element = $(this);

$.ajax({
      type: "POST",
      url: "/Controller/Action",
      data: { a: a, b: b },
      success: function (data) {
       // ....................
               target_element.parent()
     ...............................

Put the call to ajax in a function, and pass an element argument to the function?

function doAjax(triggerElement)
{
   $.ajax({
      url: whatever,
      context: triggerElement,
      success : function (content) { $(this).html(content);  }
   });
}

$(function () {

   $('#triggerElementId').click(function () { doAjax(this); });

});