简体   繁体   English

如何检查会话存储中是否存在某个项目?

[英]how to check if an item exist in sessionstorage?

I am creating a web page where the user can add an item into a dropbox buy clicking a button. 我正在创建一个网页,用户可以在其中单击按钮将项目添加到保管箱。 The sessionstorage store the partnum and quantity of the item. 会话存储存储项目的零件号和数量。 The dropbox will display the details (quantity would be 1)of the item selected. 下拉框将显示所选项目的详细信息(数量为1)。 How do I update the quantity to 2 if the same item is selected? 如果选择了相同的项目,如何将数量更新为2?

        $("#btnBuy0").click(function()
        {
            $("#dropbox").append('<span><img class = "thumb" src="../images/21_metoyou.jpg" />' + teddy[0].desc + ", Price £"
             + teddy[0].price + ", Quantity: " + quantity + "</span><br/>");
            if (Modernizr.sessionstorage) 
            {  // check if the browser supports sessionStorage
                myids.push(teddy[0].partnum + quantity); // add the current username to the myids array
                sessionStorage["ids"]=JSON.stringify(myids); // convert it to a string and put into sessionStorage
            }   
            else 
            {
             // use cookies instead of sessionStorage
            }
            for (var item =0; item<sessionStroage.length; item++)
            {
                var key = sessionStorage.key(teddy[0].partum);
                if (teddy[0].partnum == teddy[item].partnum)
                {
                var q = sesstionStorage.getItem(quantity, quantity++);
                }

I would suggest you make use of a differnt data structure for storing the user's basket. 我建议您利用不同的数据结构来存储用户的购物篮。 Instead of using an Array (myids), you could make use of an Associative Array (by using a JavaScript object) to map the partnum against a quantity, eg: 您可以使用关联数组 (通过使用JavaScript对象)来partnum (例如partnum )来将partnum映射为数量,例如:

// Basket is initially empty.
basket = {};

function saveOrder(teddy, quantity) {
    var partnum = teddy[0].partnum;

    // Create a mapping between the partnum and the quantity
    basket[partnum] = quantity;

    // Write the basket to sessionStorage.
    sessionStorage.basket = JSON.stringify(basket);
}

Using a map would allow you to create helper methods to read and write the basket object from SessionStorage, eg: 使用映射将允许您创建帮助方法来从SessionStorage读取和写入篮子对象,例如:

function fetchBasketFromSession() {
    return JSON.parse(sessionStorage.basket);
}

function writeBasketToSession(basket) {
    sessionStorage.basket = JSON.stringify(basket)
}

function getPartNumOf(teddy) {
    return teddy[0].partnum;
}

function getQuantityInSessionBasketOf(teddy) {
    // Fetch the basket from sessionStorage
    var sessionBasket = fetchBasketFromSession(),
        partnum = getPartNumOf(teddy);

    // Return the quantity mapped to the partnum in the basket, or 0 if nothing
    // is mapped.
    return sessionBasket[partnum] || 0;
}


// Combining these functions would allow you to update the users basket.
function addToBasket(teddy, quantityToAdd) {
    var sessionBasket = fetchBasketFromSession(),
        currentQuantity = getQuantityInSessionBasketOf(teddy),
        partnum = getPartNumOf(teddy);

    // Update the quantity for this partnum and write it back out.
    sessionBasket[partnum] = currentQuantity + quantityToAdd;
    writeBasketToSession(sessionBasket);
}

Hope that helps :) 希望有帮助:)

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

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