简体   繁体   English

Ajax调用后更新div

[英]update div after ajax call

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

I have the above code which opens a modal box, a href inside slidera_img div makes a ajax get with following code. 我上面的代码打开了一个模式框,slidera_img div内的href使以下代码获得ajax。 After that I want to be able to update the div with slidera_num class to the current value i get from the ajax request. 之后,我希望能够将带有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);
        });
    }

I tried above but no luck. 我尝试以上但没有运气。

How can i do this? 我怎样才能做到这一点?

Thanks. 谢谢。

first just link to the image you need: 首先只需链接到您需要的图像:

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

then call fancybox to display it. 然后调用fancybox进行显示。 on that call you can hook with the onComplete event that returns the item and then call any other ajax call you need to update the page. 在该调用上,您可以挂钩onComplete事件,该事件返回该项目,然后调用您需要更新页面的任何其他ajax调用。 so just call it with: 因此只需调用:

$("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);
    });
}});

Without knowing how fancybox works exactly, I can't help you much, but I can suggest some things: 不知道fancybox的工作原理如何,我无法为您提供很多帮助,但是我可以提出一些建议:

  1. Check in your browser if there's a javascript error. 检查您的浏览器是否存在javascript错误。 If either ajaxpost() or the ajax callback function fails, your code will not work. 如果ajaxpost()或ajax回调函数失败,则您的代码将无法工作。
  2. Open your page in a browser with a debugger, for example a webkit browser with Web Inspector or Firefor with Firebug, and put a breakpoint in your ajaxpost() and the ajax callback, one in each. 在带有调试器的浏览器中打开页面,例如,具有Web Inspector的Webkit浏览器或具有Firebug的Firefor,然后在ajaxpost()和ajax回调中分别放置一个断点。 Check if you are selecting correct elements. 检查您是否选择了正确的元素。 Is item what you expect it to be? 项目是您期望的吗? Is the selector chain in your ajax callback finding what you are expecting it to find? Ajax回调中的选择器链是否在寻找您期望的内容?
  3. Tell what the original ajaxpost() looks like and what exactly you added. 告诉您原始的ajaxpost()是什么样的,以及您添加的内容是什么。 Also tell us what your html is transformed into when your fancybox is loaded. 还请告诉我们您的fancybox加载时html会转换成什么。 You can also see this in Web Inspector or Firebug. 您也可以在Web Inspector或Firebug中看到它。 With this info, someone may be able to help. 有了此信息,也许有人可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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