使用 jquery 添加到购物车并插入标签

发布于 2025-01-01 17:19:42 字数 4159 浏览 0 评论 0原文

我有这段代码来实现一些电子商务:

$(document).ready(function(){ 

$("#basketItemsWrap li:first").hide();

$(".productPriceWrapRight a img").click(function() {
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[1];

    var productX        = $("#productImageWrapID_" + productIDVal).offset().left;
    var productY        = $("#productImageWrapID_" + productIDVal).offset().top;

    if( $("#productID_" + productIDVal).length > 0){
        var basketX         = $("#productID_" + productIDVal).offset().left;
        var basketY         = $("#productID_" + productIDVal).offset().top;         
    } else {
        var basketX         = $("#basketTitleWrap").offset().left;
        var basketY         = $("#basketTitleWrap").offset().top;
    }

    var gotoX           = basketX - productX;
    var gotoY           = basketY - productY;

    var newImageWidth   = $("#productImageWrapID_" + productIDVal).width() / 3;
    var newImageHeight  = $("#productImageWrapID_" + productIDVal).height() / 3;

    $("#productImageWrapID_" + productIDVal + " img")
    .clone()
    .prependTo("#productImageWrapID_" + productIDVal)
    .css({'position' : 'absolute'})
    .animate({opacity: 0.4}, 100 )
    .animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
                                                                                                                                                                                                                                                                                                                $(this).remove();

        $("#notificationsLoader").html('<img src="images/loader.gif">');

        $.ajax({  
            type: "POST",  
            url: "funzioni-carrello.asp",  
            data: { productID: productIDVal, action: "addToBasket"},  
            success: function(theResponse) {

                $("#empty").hide();

                if( $("#productID_" + productIDVal).length > 0){
                    $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
                    $("#productID_" + productIDVal).before(theResponse).remove();
                    $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
                    $("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
                    $("#notificationsLoader").empty();
                } else {
                    $("#basketItemsWrap li:first").before(theResponse);
                    $("#basketItemsWrap li:first").hide();
                    $("#basketItemsWrap li:first").show("slow");  
                    $("#notificationsLoader").empty();          
                }

            }  
        });  

    });

});
$("#basketItemsWrap li img").live("click", function(event) { 
    if ($("#basketItemsWrap li").length == 1){
           $("#empty").show();
       }
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[1];  

    $("#notificationsLoader").html('<img src="images/loader.gif">');

    $.ajax({  
        type: "POST",  
        url: "funzioni-carrello.asp",  
        data: { productID: productIDVal, action: "deleteFromBasket"},  
        success: function(theResponse) {

            $("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
            $("#notificationsLoader").empty();

        }  
    });  

});

});

然后我有这样的函数调用:

            <div id="basketWrap">
            <div id="basketTitleWrap">
                <h2>Carrello </h2>
            </div>
            <div id="empty">Your basket is empty!</div>
            <div id="basketItemsWrap">
                <ul>
                <li></li>
                <% 
                'dim getBasket
                getBasket() %>

              </ul>
            </div>
        </div> 

当至少有 1 个项目时,我想插入一个图像“结帐”来链接另一个页面。这可能吗?

I've this code to implement a little ecommerce:

$(document).ready(function(){ 

$("#basketItemsWrap li:first").hide();

$(".productPriceWrapRight a img").click(function() {
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[1];

    var productX        = $("#productImageWrapID_" + productIDVal).offset().left;
    var productY        = $("#productImageWrapID_" + productIDVal).offset().top;

    if( $("#productID_" + productIDVal).length > 0){
        var basketX         = $("#productID_" + productIDVal).offset().left;
        var basketY         = $("#productID_" + productIDVal).offset().top;         
    } else {
        var basketX         = $("#basketTitleWrap").offset().left;
        var basketY         = $("#basketTitleWrap").offset().top;
    }

    var gotoX           = basketX - productX;
    var gotoY           = basketY - productY;

    var newImageWidth   = $("#productImageWrapID_" + productIDVal).width() / 3;
    var newImageHeight  = $("#productImageWrapID_" + productIDVal).height() / 3;

    $("#productImageWrapID_" + productIDVal + " img")
    .clone()
    .prependTo("#productImageWrapID_" + productIDVal)
    .css({'position' : 'absolute'})
    .animate({opacity: 0.4}, 100 )
    .animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
                                                                                                                                                                                                                                                                                                                $(this).remove();

        $("#notificationsLoader").html('<img src="images/loader.gif">');

        $.ajax({  
            type: "POST",  
            url: "funzioni-carrello.asp",  
            data: { productID: productIDVal, action: "addToBasket"},  
            success: function(theResponse) {

                $("#empty").hide();

                if( $("#productID_" + productIDVal).length > 0){
                    $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
                    $("#productID_" + productIDVal).before(theResponse).remove();
                    $("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
                    $("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
                    $("#notificationsLoader").empty();
                } else {
                    $("#basketItemsWrap li:first").before(theResponse);
                    $("#basketItemsWrap li:first").hide();
                    $("#basketItemsWrap li:first").show("slow");  
                    $("#notificationsLoader").empty();          
                }

            }  
        });  

    });

});
$("#basketItemsWrap li img").live("click", function(event) { 
    if ($("#basketItemsWrap li").length == 1){
           $("#empty").show();
       }
    var productIDValSplitter    = (this.id).split("_");
    var productIDVal            = productIDValSplitter[1];  

    $("#notificationsLoader").html('<img src="images/loader.gif">');

    $.ajax({  
        type: "POST",  
        url: "funzioni-carrello.asp",  
        data: { productID: productIDVal, action: "deleteFromBasket"},  
        success: function(theResponse) {

            $("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
            $("#notificationsLoader").empty();

        }  
    });  

});

});

and Then i've the function call like this:

            <div id="basketWrap">
            <div id="basketTitleWrap">
                <h2>Carrello </h2>
            </div>
            <div id="empty">Your basket is empty!</div>
            <div id="basketItemsWrap">
                <ul>
                <li></li>
                <% 
                'dim getBasket
                getBasket() %>

              </ul>
            </div>
        </div> 

I would like to insert an image "checkout" to link another page, when there is at least 1 item. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2025-01-08 17:19:42
// JQuery
if ($("#basketItemsWrap li").length > 0){
  // Reference to a hidden checkout link
  $("#checkout").show();
}
else{
  $("#checkout").hide();    
}

<!-- HTML -->
<a id="checkout" href="/my/checkout/page">
  <img alt="Checkout" src="/my/checkout/image" />
</a>

/* CSS */
#checkout{
  visible: none;
}

每次用户添加新商品时,您都会检查购物篮中元素的数量(li elements)

// JQuery
if ($("#basketItemsWrap li").length > 0){
  // Reference to a hidden checkout link
  $("#checkout").show();
}
else{
  $("#checkout").hide();    
}

<!-- HTML -->
<a id="checkout" href="/my/checkout/page">
  <img alt="Checkout" src="/my/checkout/image" />
</a>

/* CSS */
#checkout{
  visible: none;
}

You will check the quantity of elements in basket (li elements) every time user adds a new item

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文