1

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?

4

2 に答える 2

2

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.

于 2013-02-13T08:18:30.703 に答える
1

just use the jquery 'on' event. Attach a new attribute called data-session to the div and then retrieve it using .attr method. You have the example bellow to show you an alert with the data, you just have to substitute it with your code

<html>
<head>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
   <script>
      $(document).ready(function(){
         $(document).on('click', '#follow-me-button', function(){
            alert($(this).attr('data-session'));
         })
      })
   </script>
</head>
<body>
   <div id="follow-me-button" class='btn btn-mini follow-btn' data-status='follow' data-session ="MY-SESSION-DATA-FROM-PHP">
            Follow
    </div>
</body>
</html>
于 2013-02-13T08:19:24.263 に答える