Javascript 购物车需要帮助
我有一个包含产品列表的 gridview,需要将它们添加到 javascript 购物车中。 使用以下代码可以完美地工作:
<a href="javascript:;" onclick="simpleCart.add( 'name=<%# Eval("Name") %>' , 'price=<%# Eval("Price") %>' , 'quantity=1' );">Add To Cart</a>
但是,当产品添加到购物车时,我还需要执行一些服务器端功能,因此我想用按钮替换上面的内容并像这样调用 onClick 和 onClientClick:
<asp:Button ID="AddCartButton" runat="server" Text="Add To Cart" onClick="AddCartButton_Click" OnClientClick="AddItem()" />
<script type="text/javascript">
function Additem() {
javascript: simpleCart.add('name=<%# Eval("Name") %>', 'price=<%# Eval("Price") %>', 'quantity=1');
}
</script>
这不会产生任何错误,但 JavaScript 似乎无法正常工作(因为没有产品添加到购物车),但服务器端代码执行正常。
I have a gridview with a list of products and need to add these to a javascript shopping cart.
Using the following code this works perfectly:
<a href="javascript:;" onclick="simpleCart.add( 'name=<%# Eval("Name") %>' , 'price=<%# Eval("Price") %>' , 'quantity=1' );">Add To Cart</a>
However, I also need to perform some server-side functions when a product is added to cart so I thought of replacing the above with a button and calling onClick and onClientClick like so:
<asp:Button ID="AddCartButton" runat="server" Text="Add To Cart" onClick="AddCartButton_Click" OnClientClick="AddItem()" />
<script type="text/javascript">
function Additem() {
javascript: simpleCart.add('name=<%# Eval("Name") %>', 'price=<%# Eval("Price") %>', 'quantity=1');
}
</script>
This doesn't produce any errors, but the javascript doesn't seem to be working (as no products are added to the cart), however the server-side code is executing properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的情况下,您正在创建很多全局函数,并且只会使用最新的函数。为您的数据提供此函数的参数,并仅在页面上声明一次。
更改为:
并且超出数据范围:
In your case you are creating a lot of global functions and it will be used only latest one. Provide your data with params to this function and declare it only once on the page.
Change to:
and outside of data boundind:
我必须将 onClientClick 更改为:
并将函数更改为:
正如 Samich 上面提供的那样。
I had to change the onClientClick to this:
And the function to:
As Samich provided above.