如何使用onclick标签发送post值ajax?
我像这样创建代码,但我没有任何发布值
我该怎么做?
...................................................... ...................................................... ...................................................... ......................
<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>
最佳答案
实际上你做错了,你应该像这样定义文档就绪函数
$(document).ready(function(){ ....
然后你不应该通过任何函数调用传递任何值或赋予 html onclick
属性,因为 jQuery 本身足以为你完成这项工作。请参阅下面的代码:
<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>
你的 HTML 可以是这样的
<a class="like" style="cursor: pointer;" id="99999999999999">Click here</a>
关于javascript - 如何使用onclick标签发送post值ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187381/