javascript - 如何获取一组 li 元素并将其放入 JavaScript 中的对象中

标签 javascript jquery html css local-storage

我首先要征求意见。我只在 JavaScript 上制作购物车,我想知道如何更正确地组织我的应用程序。例如我有两个页面:产品页面和购物篮页面。为了存储数据,我使用数组:

var productId, productName, productSize, productAmount;
var productsData = [];

我想把这样的对象放在那里:

productsData.push({productId:productId,productName:productName,productSize:productSize,productAmount:productAmount});

我不知道如何获取一组 li 元素并将它们放入我的对象中(存储了每种尺寸的鞋子)。 我创建了下一个,但不幸的是它不起作用:

$('.size').on("click", "a li", function () {
    productSize =  parseInt($(this).text(), 10);
    productsData.push({productSize:productSize});
    console.log(productsData[productId]['productSize']);
});

当我获得整个对象时,我会将其放入本地存储并显示在购物车页面上。

在我的 html 代码片段下面:

<main class="product-section">
        <div class="product-preview" id="item1">
            <div class="current-photo-product">
                <img src="img/product-photo1.png" alt="Grey">
                <img src="img/product-photo2.png" alt="White">
                <img src="img/product-photo3.png" alt="Light Blue">
                <img src="img/product-photo4.png" alt="Dark Blue">
            </div>
            <div class="choice-photo">
                <ul>
                    <li><img src="img/product-photo1.png" alt="Grey"></li>
                    <li><img src="img/product-photo2.png" alt="White"></li>
                    <li><img src="img/product-photo3.png" alt="Light Blue"></li>
                    <li><img src="img/product-photo4.png" alt="Dark Blue"></li>
                </ul>
            </div>
        </div>
        <div class="product-details">
            <div class="wrapper-details">
                <h2>New Balance</h2>
                <p class="article-number">Article number: 14210160762</p>
                <h4 id="amount">€ 99.95</h4>
                <p class="description">Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies.
                </p>
                <h3>Size</h3>
                <ul class="size" id="listOfSizes">
                    <a href="#"><li>38</li></a>
                    <a href="#"><li>39</li></a>
                    <a href="#"><li>40</li></a>
                    <a href="#"><li>41</li></a>
                    <a href="#"><li>42</li></a>
                </ul>
                <button id="add-to-cart">Add to cart</button>
            </div>
        </div>
    </main>

最佳答案

我不太清楚你的问题,但让我告诉你如何使用你建议的对象数组 {productId:productId,productSize:productSize,...} - 也许吧可以帮助你:

当你在这样的数组中创建一个对象,并且你想添加一个属性,比如 productSize,productName,...你必须确保你'重新将它添加到对象中,而不是像使用 productsData.push({productSize:productSize}); 那样添加到数组中。 因此,首先通过属性 productId 找到正确的对象,然后添加您的属性和值对。

 productsData = [];

//random id for this example - you can generate those however you like of course
productId = 112;

//you add the new product with given id...but you don't know how big the array was before
productsData.push({productId:productId})

//now you set a size
productSize = 33;

//you search for the product with given ID in the array to be able to add the size value to it
//the method "arrayObjectIndexOf" I just copied from an answer here: 
//http://stackoverflow.com/questions/8668174/indexof-method-in-an-object-array
var searchIndex = arrayObjectIndexOf(productsData, productId, "productId");
//now in the first brackets you get inside the object and in the second you create a new property "productSize"and set its value
productsData[searchIndex]["productSize"] = productSize;

alert(productsData[searchIndex].productSize);

function arrayObjectIndexOf(myArray, searchTerm, property) {
    for(var i = 0, len = myArray.length; i < len; i++) {
        if (myArray[i][property] === searchTerm) return i;
    }
    return -1;
}

关于javascript - 如何获取一组 li 元素并将其放入 JavaScript 中的对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379555/

相关文章:

javascript - shadow DOM 中的 addEventListener

javascript - Rhino 将 java.lang.String 转换为 JavaScript 字符串

javascript - 根据文本值禁用复选框

javascript - 如何创建静态 <div>?

jquery - 将导航栏固定在 Bootstrap 的特定位置

javascript - HTML5 Canvas 触摸操纵杆性能问题

jquery - 如何使用 <li> 标签在每个新行中换行 HTML 文本?

javascript - 在 JS 对象中引用 HTML 元素

javascript - Div 在 Safari 10.0.1 上未正确呈现(就像是否存在溢出 :hidden somewhere but there isn't)

html - z 索引溢出