简体   繁体   English

如何使用onclick标记发送帖子值ajax?

[英]How to send post value ajax using onclick a tag?

How to send post value ajax using onclick a tag ? 如何使用onclick标记发送帖子值ajax?

i create like this code but i not have any post value 我创建像这样的代码,但我没有任何发布价值

how can i do that ? 我怎样才能做到这一点 ?

..................................................................................................................................................................... .................................................. .................................................. .................................................. ...............

<script>
    $(document).ready(function reply_click(clicked_id){
        $('body').on('click','.like',function(){    
            var pro_id = $(this).attr('clicked_id');        
            var postData = 'pro_id='+pro_id;
            $.ajax({        
                type: "POST",
                url: "receiver.php",
                data: postData,
                cache: false,
                success: function(){            
                    $('#'+pro_id).html('OK').addClass('unlike').removeClass('like');
                }       
            });
        })
    });
</script>


<a class="like" onClick="reply_click(this.id)" style=" cursor: pointer; " id="99999999999999">click here.</a>

Actually you are doing it wrong, you are supposed to define document ready function like this 实际上您做错了,应该像这样定义文档准备功能

$(document).ready(function(){ ....

and then you are not supposed to pass any value or give to html onclick attribute with any function call, since jQuery itself is enough to do this job for you. 然后您不应该通过任何函数调用传递任何值或给html onclick属性,因为jQuery本身足以为您完成此工作。 See my code below: 请参阅下面的代码:

<script>
    $(document).ready(function(){
        $('body').on('click','.like',function(){    
            var pro_id = $(this).attr('id');        
            var postData = 'pro_id='+pro_id;
            $.ajax({        
                type: "POST",
                url: "receiver.php",
                data: postData,
                cache: false,
                success: function(){            
                    $('#'+pro_id).html('OK').addClass('unlike').removeClass('like');
                }       
            });
        })
    });
</script>

Your HTML can be like this 您的HTML可以像这样

<a class="like" style="cursor: pointer;" id="99999999999999">Click here</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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