简体   繁体   English

使用AJAX显示SQL查询结果

[英]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)。


The Problem 问题
I would like to display the users, who liked a post using ajax. 我想显示喜欢使用ajax发布信息的用户。 Whenever a certain element is hovered. 每当将某个元素悬停时。
At the moment the users are correctly displayed but below every post, not just the one which contains the hovered element. 目前,用户已正确显示,但在每个帖子下方,而不仅仅是包含悬停元素的帖子。
My attempt to solve the problem 我解决问题的尝试

 <!-- 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> 


I would be very thankful for any kind of help! 我将非常感谢您提供的任何帮助!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM