简体   繁体   English

Ajax调用后的Javascript目标

[英]Javascript target after ajax call

I am a little confused and the problem can probably be resolved in a matter of seconds for someone in the know. 我有些困惑,对于一个知道的人来说,问题可能会在几秒钟内解决。

I have a product viewing page that has the ability to add to cart. 我有一个产品查看页面,可以添加到购物车。 Once the user clicks "add to cart" it executes an ajax script and runs an asp page in the background to add the product to the cart database table. 一旦用户点击“添加到购物车”,它就会执行一个ajax脚本并在后台运行一个asp页面,将产品添加到购物车数据库表中。 once this happens I count the number of total products in the cart with a simple function and want to display it on the page automatically. 一旦发生这种情况,我会用简单的功能计算购物车中的总产品数量,并希望自动在页面上显示。 I am giving the display job to Javascript like this: 我给这样的Javascript显示工作:

<script language="javascript" type="text/javascript">
var cartnum = '&nbsp;(<%=CountItemsInCart()%>)';
top.getElementById('NumberOfItemsInCart').innerHTML = cartnum;
</script>

This is all in the ajax ASP page 这都是在ajax ASP页面中

However the javascript is failing, saying, target is NULL. 但是,javascript失败了,也就是说,目标是NULL。 How do I make the change to the default document which has the ID "NumberOfItemsInCart"? 如何更改ID为“NumberOfItemsInCart”的默认文档?

Any help you can provide would be greatly appreciated. 您将提供的任何帮助将不胜感激。

Many thanks, 非常感谢,

Paul 保罗

If you're calling the page via AJAX, this has no reference to the page you have open currently. 如果您通过AJAX调用该页面,则不会引用您当前打开的页面。 You would need to handle this in the AJAX call when it returns instead. 相反,您需要在AJAX调用中处理此问题。 You can return the Count from the ASP page, and then update the page using Javascript at that point. 您可以从ASP页面返回Count,然后在此时使用Javascript更新页面。 If you post that portion of your code, I'm sure someone could help you with the placement. 如果您将代码的这一部分发布出来,那么我相信有人可以帮助您完成展示位置。

Look into using window.opener.document. 查看使用window.opener.document。 Check here and see if that helps point you in the right direction. 点击此处查看是否有助于指明您正确的方向。

Here's a quick snippet: 这是一个快速片段:

 window.opener.document["nameForm"].getElementById("someid").value;

Like Shawn's answer, Ajax calls should only return some json/xml. 就像Shawn的答案一样,Ajax调用应该只返回一些json / xml。 You need to update your html from the callback. 您需要从回调中更新HTML。

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

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