<li>
<div class="slidera_img">
<a href="image.jpg" rel="example_group" title="<a href='#' onClick='ajaxpost(@item.Id)'><img src='foo.jpg'/></a>">
<img src="@Url.Action("ViewImage", "Image", new { id = item.Id, imageType = "thumb" })" alt="" width="100" height="100" />
</a>
</div>
<div class="slidera_num">@item.VoteCount</div>
</li>
我有上面的代码可以打开一个模态框,slidera_img div 中的一个 href 使用以下代码生成一个 ajax get。之后,我希望能够将带有 slidera_num 类的 div 更新为我从 ajax 请求中获得的当前值。
<script type="text/javascript">
function ajaxpost(id) {
var item = $(this).parent();
alert(item);
$.get('@Url.Action("VoteAjax","Home")', { id: id }, function (response) {
item.closest("li").find(".slidera_num").html(response.vote);
});
}
我在上面尝试过,但没有成功。
我该怎么做?
谢谢。
最佳答案
首先只需链接到您需要的图片:
<a href="image.jpg" rel="example_group"><img src="@Url.Action("ViewImage", "Image", new { id = item.Id, imageType = "thumb" })" alt="" width="100" height="100" /></a>
然后调用fancybox来显示它。在该调用中,您可以 Hook 返回元素的 onComplete
事件,然后调用您需要更新页面的任何其他 ajax 调用。
所以只需调用它:
$("img[rel='example_group'").fancybox({ onComplete: function(item){
$.get('@Url.Action("VoteAjax","Home")', { id: item.id }, function (response) {
$(item).closest("li").find(".slidera_num").html(response.vote);
});
}});
关于javascript - ajax调用后更新div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847467/