“目标DIV'消失在简单的jQuery AJAX调用 [英] 'Destination DIV' disappearing in simple jQuery AJAX call

查看:102
本文介绍了“目标DIV'消失在简单的jQuery AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:重要提示:这是极不可能的,任何人谁也碰到过,这将有相同的问题,因为我 - 检查我的回答如下对我非常模糊的解决方案。然而有一些有用的反应,可以适用于其他的情况。如果你有类似的问题好运气。


我有一个非常简单的jQuery AJAX调用:

  $(#车)的负载(/ woops /存储/ cartsummary)。
 

这将返回(通过一个ASP.NET MVC操作手段)一些非常简单的HTML:

 < D​​IV>东西< / DIV>
 

在我的网页我有一个目标DIV它最初使用一些HTML代码:

 < D​​IV CLASS =padding15ID =购物车>
   <%RenderCart(ViewData.Model.CartItems); %>
< / DIV>
 

现在的问题是,当调用返回原来的#cart DIV就这样消失,而不是被取代。如果我看在Chrome浏览器的DOM,这只是完全是空的。正确的HTML肯定是被退回,因为我看到它在提琴手。

此外,该code工作(但显然不是我想要的,因为它是附加):

  $。获得(/ woops /存储/ cartsummary,{},功能(数据){
    $(#车)追加(数据);
});
 

我觉得我只是在做一些愚蠢的事 - 但我从微软的AJAX切换到jQuery的,我觉得孤单只是一些小的根本的东西我的误解。哪里是我的DIV到哪里去了?

(顺便说一句:'woops是一个虚拟目录,以确保我的网站有没有牙根发育过程中相对链接)

解决方案

 追加
 

不能取代股利。它粘贴的数据在你div的背面

您可以尝试

  replaceWith(内容)
 

来代替。

replaceWith在jQuery的文档

Edit: Important: It is extremely unlikely that anybody who has come across this will have the exact same problem as me - check my answer below for my very obscure solution. There were however some useful responses that may apply to other's situations. Good luck if you're having a similar problem.


I've got a really simple jQuery AJAX call :

$("#cart").load("/woops/store/cartsummary");

This returns (by means of an ASP.NET MVC Action) some very simple HTML:

<DIV>something</DIV>

On my page I have a destination DIV which is initially populated with some HTML:

<div class="padding15" id="cart">
   <% RenderCart(ViewData.Model.CartItems); %>
</div>

The problem is when the call returns the original #cart div just disappears instead of being replaced. If I look at the DOM in Chrome it is just completely empty. The correct HTML is definitely being returned because I see it in Fiddler.

In addition this code works (but obviously isn't what I want because it is appending) :

$.get("/woops/store/cartsummary", {}, function(data) {
    $("#cart").append(data); 
});

I think I'm just doing something stupid - but I'm switching from Microsoft AJAX to jQuery and I think theres just some small fundamental thing I'm misunderstanding. Where's my DIV gone?

(BTW: 'woops' is a virtual directory to make sure my site has no root relative links during development)

解决方案

append

does not replace your div. It pastes the data at the back of your div.

You could try

replaceWith( content )

instead.

replaceWith in jQuery documentation

这篇关于“目标DIV'消失在简单的jQuery AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆