【问题标题】:How can I get set of li elements and put it to my object in JavaScript如何获取一组 li 元素并将其放在 JavaScript 中的对象中
【发布时间】:2016-05-23 17:05:05
【问题描述】:

我想征求意见的第一件事。我只在 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>

【问题讨论】:

  • 您可能还应该发布您的 HTML 片段。或者至少写一下,什么不起作用(点击后是否调用了 onclick 函数?)。
  • 我当然可以显示html片段。当我尝试添加大小时,它会抛出控制台未定义
  • a 应该在li 内,而不是其他方式。
  • ahwayakchih 我同意你的观点,但这并不能解决我的问题
  • console.log(productsData[productId]['productSize']); productId 根据您的代码未定义。当您将项目推送到它时,数组也会自动索引。所以第一项将在索引 0 处,而不是在 productId(除非它也是 0)。

标签: javascript jquery html css local-storage


【解决方案1】:

您的问题对我来说不是很清楚,但让我向您展示我将如何使用您建议的对象数组 {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;
}

【讨论】:

    【解决方案2】:

    简单,你有语法错误:

    var = productId, productName, productSize, productAmount;
    

    应该是

    var productId, productName, productSize, productAmount;
    

    您的控制台还应该处理推送的对象:

    console.log(productsData[0]['productSize']);
    

    【讨论】:

    • 我同意,谢谢!但这是我在这里犯的错误,在项目中我没有它
    猜你喜欢
    • 2021-04-09
    • 2022-12-05
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多