2009-08-07 5 views
0

Ich versuche, den Text in der mithilfe diesesAbrufen von Text mit this.text() und jquery

$(".ajax-append").click(function(event) { 
    $.get("includes/fight.php?name=test", function(html) { 

    console.log($(this).text()); 
    console.log($(this)); 

     // append the "ajax'd" data to the table body 
     $("table tbody").append(html); 
     // let the plugin know that we made a update 
     $("table").trigger("update"); 
     // set sorting column and direction, this will sort on the first and third column 
     var sorting = [[2,1],[0,0]]; 
     // sort on the first column 
     $("table").trigger("sorton",[sorting]); 
    }); 
    return false; 
}); 

das Markup wie diese

<div class="friend" id="friend6" style="padding 5px; height: 54px; margin-bottom: 10px;"> 
<div style="float: left; width: 54px;"> 
     <img style="border:2px solid #E1E1E1;" src="6.jpg"/> 
</div> 
<div id="name6" style="float: left; padding-top: 20px; padding-left: 10px; font-size: 14px; width: 200px"> 
    <a href="#" class="ajax-append" id="6">name 6</a> 
</div> 
</div> 
<div class="friend" id="friend7" style="padding 5px; height: 54px; margin-bottom: 10px;"> 
<div style="float: left; width: 54px;"> 
    <img style="border:2px solid #E1E1E1;" src="7.jpg"/> 
</div> 
<div id="name7" style="float: left; padding-top: 20px; padding-left: 10px; font-size: 14px; width: 200px"> 
    <a href="#" class="ajax-append" id="7">name 7</a> 
</div> 

sieht verknüpft klickte zu erhalten

Ich kann den Text innerhalb des Anchor-Tags erhalten, wenn ich eine bestimmte ID verwende, aber ich versuche dies mit $ (this) zu erreichen und die Alert-Nachricht ist immer undefiniert.

Kann jemand darauf hinweisen, was schief läuft? oder über einen besseren Weg nachdenken?

Danke.

Antwort

0

Dies liegt daran, dass Sie this in einem Ajax-Callback verwenden, der nicht mehr auf das angeklickte Element verweist.

+0

ich dies in Firebug diese [0] .innerHTML undefiniert [Pause zu diesem Fehler] diese [0] .innerHTML.replace (/ jQuery \ d + = "(?: \ D + | null) "/ g," "): –

4

try this:

$(".ajax-append").click(function(event) { 
    var element=this; 
    $.get("includes/fight.php?name=test", function(html) { 

    console.log($(element).text()); 
    console.log($(element)); 

     // append the "ajax'd" data to the table body 
     $("table tbody").append(html); 
     // let the plugin know that we made a update 
     $("table").trigger("update"); 
     // set sorting column and direction, this will sort on the first and third column 
     var sorting = [[2,1],[0,0]]; 
     // sort on the first column 
     $("table").trigger("sorton",[sorting]); 
    }); 
    return false; 
}); 
+0

Ich hätte es nicht besser sagen können. :-) –

+0

+1: Beat mich um 8 Sekunden. – Joel

0

Im Rahmen des Codes, ist $ (this) eine AJAX-Anforderung.

Der einfachste Weg wird sein, eine Variable vor Ihrem AJAX-Aufruf zu setzen und innerhalb von $ get zu konsumieren.

0
$(".ajax-append").each(function(){ 
    $(this).click(function(event) { 
      $.get("includes/fight.php?name=test", function(html) { 

      console.log($(this).text()); 
      console.log($(this)); 

      // append the "ajax'd" data to the table body 
      $("table tbody").append(html); 
      // let the plugin know that we made a update 
      $("table").trigger("update"); 
      // set sorting column and direction, this will sort on the first and third column 
      var sorting = [[2,1],[0,0]]; 
      // sort on the first column 
      $("table").trigger("sorton",[sorting]); 
     }); 
     return false; 
    } 
}); 
0

dieses Schlüsselwort in JavaScript wird auf dem Umfang variieren wir verwenden. Hier sind Sie in Ajax Anfrage Rückruffunktion.

So glaube ich this.responseText wird funktionieren. Check this out