dps69208 2013-02-13 08:09
浏览 40
已采纳

jQuery PHP在函数内传递值与实时点击?

I'm trying to pass some values through onclick which to me is so much faster. But I need to use some kind of "live" clicking and I was looking into .on() or .delegate(). However, if I do any of those, passing those values within followme() seems a little harder to get. Is there some kind of method that I'm not seeing?

    <div class='btn btn-mini follow-btn' 
         data-status='follow' 
         onclick="followme(<?php echo $_SESSION['user_auth'].','.$randId;?>)">
            Follow 
    </div>

function followme(iduser_follower,iduser_following)
{

        $.ajax({
        url: '../follow.php',
        type: 'post',
        data: 'iduser_follower='+iduser_follower+'&iduser_following='+iduser_following+'&unfollow=1',
        success: function(data){
                $('.follow-btn').attr("data-status",'follow');
                $('.follow-btn').text('Follow');
            }

        });

}

As you can see, its easier to just pass values from PHP to jQuery... Is there another way?

  • 写回答

2条回答 默认 最新

  • drgdn82648 2013-02-13 08:18
    关注

    You can assign more data values, and you know what use is logged in, all you need is to pass who to follow:

    <div class='btn btn-mini follow-btn' 
         data-status='follow' 
         data-who='<?php echo $randId; ?>'
         id='followBTN'>
            Follow 
    </div>
    
    <script>
        $('#followBTN').on('click', function(){
            $.ajax({
                url: '../follow.php',
                type: 'post',
                data: {
                    iduser_following: $(this).attr('data-who')
                },
                success: function(data){
                    $('.follow-btn').attr("data-status",'follow');
                    $('.follow-btn').text(data.text);
                }
    
            });
        });
    </script>
    

    You can process $_SESSION['user_auth'] and the status directly from PHP, there is no need for you to pass them in jQuery. Make sure document is ready when you insert on click event.

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

报告相同问题?