2012-01-26 64 views
1

我开始在Ajax中,我有一些问题。我有一个包含一些视频缩略图的列表页面,当您翻阅它们时,会显示一个“我不喜欢这个”图标。完成了。现在我有问题是:jQuery的Ajax - 响应应取代html

当你点击该图标,视频标题,缩略图&信息应该消失,并且必须显示另一个视频。

这里是我的Ajax代码

function ThumbsDown(id,sort,page) { 
$.ajax({ 
    url: "/change/videos/"+id+"/thumbsdown/", 
    type: "POST", 
    data: { 
     "sort": sort?sort:"", 
     "page": page?page:"" 
    }, 
    complete: function(result) { 
     // Instead of calling the div name, I need to be able to target it with $(this) and .parent() to make sure only 1 video change, but for now I only want the response to replace the video 
     $("#content .videoList ul li.videoBox").html(result);    
    } 
}); 
} 

请求工作,即时通讯刚开(200 OK)响应。它将新视频的HTML代码块返回给我。

问题是,当我点击图标时,,div中的所有html都不见了。我有一个空标签,所以我想它的解释为“空”。但在我的萤火虫.Net标签中,我清楚地看到有一个回应。

请帮忙吗? 已经定型,多亏

* *编辑**

现在即时通讯有瞄准特定的分度,$(本)和父母()的问题。有人可以帮忙吗?

我要定位的李#videoBox

<li class="videoBox recommended"> 
    <div class="spacer" style="display: block;"></div> 
    <div class="features"> 
     <div> 
     <a class="dislike_black" title="I dislike this" onclick="ThumbsDown(30835, 'relevance', '1');"></a> 
     </div> 
    ... 

我想这一点,并且它不工作。

success: function(data) { 
     $("#content .videoList ul li.videoBox").html(data); // this IS WORKING, but replacing ALL the thumbs 
     $(this).parents("li.videoBox").html(data); // THIS IS NOT 

我在做什么错?

回答

3

您遇到的问题是您正在使用“完成”回调函数。这可能会导致错误,但是回调意味着在成功或失败后被调用。这是更多的清理,并没有收到你所期望的响应数据。基本上,你所需要做的就是将“完整”改为“成功”,并且你应该收到你期望的结果。

但是,我个人不建议使用Ajax函数,因为它看起来完全有必要。 Ajax函数主要用于复杂事务,看起来你有一个相当简单的事务。我的建议,使用“快捷方式”功能,jQuery提供,像这样:

$.post("/change/videos/"+id+"/thumbsdown/", {sort:"", page:""}, function(result){ 
    // Instead of calling the div name, I need to be able to target it with $(this) and .parent() to make sure only 1 video change, but for now I only want the response to replace the video 
    $("#content .videoList ul li.videoBox").html(result); 
}, "html"); 
+0

非常感谢!不知道成功/完成的反应是不同的! – Lelly

+0

我有另一个问题,请检查我的第一篇文章吗? – Lelly

1

变化从complete to success

function ThumbsDown(id,sort,page) { 
$.ajax({ 
    url: "/change/videos/"+id+"/thumbsdown/", 
    type: "POST", 
    data: { 
     "sort": sort?sort:"", 
     "page": page?page:"" 
    }, 
    success: function(result) { 
     // Instead of calling the div name, I need to be able to target it with $(this) and .parent() to make sure only 1 video change, but for now I only want the response to replace the video 
     $("#content .videoList ul li.videoBox").html(result);    
    } 
}); 
} 

complete回调不像成功并不能得到数据参数,只参数jqXHR