[英]Display SQL Query Result using AJAX
I have a news feed on my site, onto which users can post posts. 我的网站上有一个新闻提要,用户可以在上面发布帖子。 These posts can then be liked by other users (just like facebook for example). 这些帖子然后可以被其他用户喜欢(例如,像facebook)。
<!-- HOVER THIS --> <span class="likers small link"></span> <!-- DISPLAY LIKERS HERE --> <small class="displayLikers"></small> <!-- AJAX --> <script> $(function() { $(".likers").hover(function(){ $.ajax({ url: "get/likers", type: "GET", success: function(response) { $(this).closest('.displayLikers').html(response); } }); }); }); </script>
Inside the $.ajax
, $(this)
does not refer to $(".likers")
just add $(this)
to a variable and use it in the ajax response function; 在$.ajax
内部, $(this)
并不引用$(".likers")
只需将$(this)
添加到变量中并在ajax响应函数中使用它即可;
$(function() {
$(".likers").hover(function(){
var likes = $(this);
$.ajax({
url: "get/likers",
type: "GET",
success: function(response) {
likes.closest('.displayLikers').html(response);
}
});
});
});
In your example the .displayLikers
is a sibling so you should probably use next()
. 在您的示例中, .displayLikers
是兄弟姐妹,因此您可能应该使用next()
。 Moreover this
will refer to the actual context of the success function, so you have to create a reference before. 此外, this
将引用成功函数的实际上下文,因此您必须在之前创建引用。
$(function() {
$(".likers").hover(function(){
var self = $(this);
$.ajax({
url: "get/likers",
type: "GET",
success: function(response) {
self.next('.displayLikers').html(response);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.