Jquery 如何在当前子元素中加载ajax响应?
我正在尝试在当前div项的子元素中添加ajax加载的内容,但它不起作用,无法找出问题所在。Jquery 如何在当前子元素中加载ajax响应?,jquery,ajax,Jquery,Ajax,我正在尝试在当前div项的子元素中添加ajax加载的内容,但它不起作用,无法找出问题所在。 下面是HTML代码: <div class="thumb-pitch"> <span class="pitch-count" ></span> </div> <div class="thumb-pitch"> <span class="ditch-count"></span> </div> JS代
下面是HTML代码:
<div class="thumb-pitch">
<span class="pitch-count" ></span>
</div>
<div class="thumb-pitch">
<span class="ditch-count"></span>
</div>
JS代码:
$('.thumb-pitch').click(function() {
var id= $(this).attr('cid');
var dataString = 'idea_id='+ id;
$.ajax({
type: 'POST',
url:"<?php echo base_url();?>users/user_votes/user_pitch",
data:dataString,
success: function(response) {
//alert(response);
$(this).find('span').html(response);
}
});
});
$('.thumb-pitch')。单击(函数(){
var id=$(this.attr('cid');
var dataString='idea_id='+id;
$.ajax({
键入:“POST”,
url:“用户/用户投票/用户音调”,
数据:dataString,
成功:功能(响应){
//警报(响应);
$(this.find('span').html(响应);
}
});
});
您需要将$(this)
的引用存储在变量中,然后在suces回调处理程序中使用该变量
使用
$('.thumb-pitch')。单击(函数(){
var id=$(this.attr('cid');
var$this=$(this);//将此引用存储在变量中
$.ajax({
键入:“POST”,
url:“用户/用户投票/用户音调”,
数据:{“idea_id”:id},
成功:功能(响应){
//警报(响应);
$this.find('span').html(response);//在此处使用引用变量
}
});
});
您可以为此使用children()
$(this.children('span').html(response) ajax响应是否正常?url中的用户前面是否有一个
/
,并且$(这)不是您所认为的。请共享响应对象的详细信息?
$('.thumb-pitch').click(function() {
var id = $(this).attr('cid');
var $this = $(this); //Store this reference in a variable
$.ajax({
type: 'POST',
url: "<?php echo base_url();?>users/user_votes/user_pitch",
data: {"idea_id" : id},
success: function(response) {
//alert(response);
$this.find('span').html(response); //Use the reference variable here
}
});
});