doumowu7371 2013-12-13 15:20
浏览 43
已采纳

如何在PHP中生成唯一的LIKES id

I have a button in my page:

if($posts = $q->fetchAll()) {
    foreach ($posts as $post) {
        $username = $post[0];
        $post_id = $post[2];
        $status = $post[1];
        echo $username . " " . $status . "<br/>";
        echo "<button value = '$post_id' id = 'like' class='like' type='submit'>Like</button>";
    }
}

Let's assume I have ten result from the query, I will definitely have 10 LIKE button with the same ID.

Now my jQuery is coming this way:

$("#like").click(function() {  
    var menuId = $(this).val();

    var request = $.ajax({
        url: "likes.php",
        type: "POST",
        data: { id : menuId },
        dataType: "html"
    });

    request.done(function( msg ) {
        $(".likecount").html( msg ); 
    });
});  

Every click on a single button applies to all 10 buttons. How do I differentiate them and have the database affected accordingly?

  • 写回答

3条回答 默认 最新

  • douzhang6176 2013-12-13 15:30
    关注

    First off, you're adding like elements via ajax, but you're binding the event handler to whichever like element is already part of the dom at any given moment:

    $(document).ready(function()
    {
        //when dom is loaded, #like is selected, and event is bound
        $('#like').click(function(){});
    });
    

    inside the click handler, you perform an ajax call that may add another like element to the page, but you never bind an event handler to that new element.
    You have 2 options: add an event handler for each element that is added to the page dynamically (not so good, bad for performance). OR delegate the event. As an added bonus, you don't need the ID's of the like buttons anymore. You can use the like class to delegate!

    $(document).ready(function()
    {
        $('body').on('click', '.like', function()
        {
            //handle click on like button here
        });
    });
    

    This adds an event listener to the body tag, that will call the callback function whenever a click is registered on an element that has the like class.

    I'll edit this response, to give you a, purely hypothetical way to ensure unique like id's


    Using a closure, you can easily get unique ID's, by exploiting the fact that closure vars can outlive the closure function. But as you can see, just from the verbosity and added complexity of the code below, this approach is not to be recommended. Simply use the class, and leave the ID out. delegation all the way!

    $('body').on('click', '.like', (function(count)
    {//closure, pass like buttons currently on page
        var idNum = 0;
        count.each(function()
        {
            $(this).attr('id', $(this).attr('id') + idNum);
            ++idNum;//increment
        });
        return function()
        {//this is the actual callback
             request.done(function( msg )
             {
                 var chunk = $(msg);//parse HTML response
                 chunk.find('.like').each(function()
                 {
                     $(this).attr('id', ($(this).attr('id') || 'like') + idNum);
                     ++idNum;
                 });
             });
        };
    }($('.like'))));
    

    Or, if for some reason you don't want to delegate the event:

    $('.like').on(function handler()
    {//callback should be named, you'll see why
        request.done(function( msg )
        {
            $('.like').off('click', handler);//remove handler
            //add msg to DOM
            $('.like').on('click', handler);//add handler, now including new DOM elements
        });
    });
    

    You could (and IMO should) optimize this further, by storing the $.each callback in a closure reference, too:

    $('body').on('click', '.like', (function(count)
    {//closure, pass like buttons currently on page
        var idNum = 0, eachCallback = function()
        {
            $(this).attr('id', ($(this).attr('id') || 'like') + idNum);
            ++idNum;//increment
        };
        count.each(eachCallback);
        return function()
        {//this is the actual callback
             request.done(function( msg )
             {
                 var chunk = $(msg);//parse HTML response
                 chunk.find('.like').each(eachCallback);
             });
        };
    }($('.like'))));
    

    This way, you avoid creating a callback function object on each click event... but read up on closures to fully understand why this is a better approach, performance wise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大