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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配