[英]After calling jQuery load or $.ajax with datatype html element does not update
I am currently having a problem where when the jQuery Load is called an element which was previously updateable is no longer updateable it reverts to its original state and although it is still accessible and updateable the visual on the screen does not change. 我目前遇到一个问题,当调用jQuery Load时以前无法更新的元素不再可更新时,它会恢复到其原始状态,尽管它仍然可以访问和更新,但屏幕上的视觉效果不会改变。
The element is in the a partial view in the layout page. 该元素位于布局页面的局部视图中。 While the load event is being called from a click in a <li>
menu item. 通过单击<li>
菜单项中的调用加载事件时。 Which then calls load and attempts to get the possible changes in cart. 然后调用load并尝试获取购物车中可能的更改。
Partial View Code 部分查看代码
<div id="cart" style="float:right;clear:right;margin-top:31px;height:21px;">
<span>
<a href='/ShoppingCart/ShoppingCart'><img style='height:16px;margin-bottom:5px;' src='../Content/images/cartImage.gif' alt='cart' />0 Total : 0</a>
</span>
</div>
Layout Code 布局代码
function GetCart() {
var url = "/ShoppingCart/GetCartAmount/";
$.ajax({
url: url,
data: {},
cache: false,
type: "POST",
success: function (result) {
var markup = "<span> <a href='/ShoppingCart/ShoppingCart'><img style='height:16px;margin-bottom:5px;' src='../Content/images/cartImage.gif' alt='cart'/></a> " +
result.ItemCount + " Total: " + result.Total + "</span>";
$("#cart").html(markup);
alert($('#cart').html());
alert("after set");
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
The Ajax code is in a function with other processing. Ajax代码与其他处理一起使用。
$.ajax({
url: data.redirectUrl,
dataType: "html",
success: function (responseText) {
var tempDiv = $.parseHTML(responseText);
$("#result").html(tempDiv).show();
}
});
GetCart();
Controller Code 控制器代码
<HttpPost()>
Function GetCartAmount() As ActionResult
Dim ItemCount = GetCart().Count
Return Json(New With {.Total = GetCartTotal().ToString, .ItemCount = ItemCount})
End Function
Public Function GetCart() As List(Of WebItemModel)
Dim retValue As New List(Of WebItemModel)
If Session("Cart") IsNot Nothing Then
retValue = DirectCast(Session("Cart"), List(Of WebItemModel))
End If
Return retValue
End Function
If any further code should be provided please do tell me. 如果需要提供其他代码,请告诉我。 I didn't post it all since it would take up too much space and possibly confuse people. 我没有发布所有内容,因为这会占用太多空间并可能使人们感到困惑。 Also to make a point it initially works before the jQuery load occurs. 也要指出一点,它最初在jQuery加载发生之前就起作用。
I finally got it working by doing the following 我终于通过执行以下操作
function GetCart() {
var url = "/ShoppingCart/Cart/";
var $partialDiv = $('#PartialCart div')
$.get(url, function (data) {
$partialDiv.replaceWith(data);
});
}
It seems to be working perfectly now the replaceWith did the trick for me. 现在似乎replacep可以正常工作了。
Controller Code 控制器代码
Public Function GetCart() As List(Of WebItemModel)
Dim retValue As New List(Of WebItemModel)
If Session("Cart") IsNot Nothing Then
retValue = DirectCast(Session("Cart"), List(Of WebItemModel))
End If
Return retValue
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.