简体   繁体   中英

How to send post value ajax using onclick a tag?

How to send post value ajax using onclick a tag ?

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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