最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select parent of $.ajax Jquery? - Stack Overflow

programmeradmin0浏览0评论

How can I select parent element of $.ajax, or parent element of the element that triggered $.ajax. I need some kind of reference so I can apply result data to it:

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
});   

$(this).parent() in ajax success function returns jQuery() Any ideas?

How can I select parent element of $.ajax, or parent element of the element that triggered $.ajax. I need some kind of reference so I can apply result data to it:

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
});   

$(this).parent() in ajax success function returns jQuery() Any ideas?

Share Improve this question asked Jan 24, 2012 at 9:09 Davor ZubakDavor Zubak 4,74614 gold badges61 silver badges95 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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); });

});
发布评论

评论列表(0)

  1. 暂无评论