简体   繁体   English

更新 sessionStorage 中 object 中的值

[英]Update values in object in sessionStorage

I have this object stored in sessionstorage:我有这个 object 存储在会话存储中:

0: {FromName: "FName", FromLastName: "", FromAddress: "F Street", FromCountry: "Portugal", FromCity: "FC",…}
1: {FromName: "FName", FromLastName: "", FromAddress: "F Street", FromCountry: "Portugal", FromCity: "FC",…}
2: {FromName: "", FromLastName: "", FromAddress: "", FromCountry: "", FromCity: "", FromPostCode: "",…}

It displays a list of Shopping Cart Items.它显示购物车项目列表。

When I'm at the Cart, I have a button that makes me able to display the values in a form and it's already getting the right values from the sessionStorage.当我在购物车上时,我有一个按钮,它使我能够在表单中显示值,并且它已经从 sessionStorage 中获取了正确的值。

The question is, how can I update the values, using the displayed form and the updated the object correctly?问题是,如何使用显示的表格和正确更新的 object 更新值?

This is the code that grabs the correct values from the object in sessionstorage:这是从 sessionStorage 中的 object 中获取正确值的代码:

  e.preventDefault();

  var itemsInCart = JSON.parse(sessionStorage.getItem('productList'));
  var ID = $(this).data('id');

  for (var i = 0; i < itemsInCart.length; i++) {

    var itemsinCartID = itemsInCart[i].keyID;

    if (ID == itemsinCartID) {

    $('.product-image').attr('src', itemsInCart[i].Product);
    $('#message').val(itemsInCart[i].Message);

    // from inputs
    var fromName = $('#from-name-input').val(itemsInCart[i].FromName);
    var fromLastName = $('#from-last-name-input').val(itemsInCart[i].FromLastName);
    var fromAddress = $('#from-address-input').val(itemsInCart[i].FromAddress);
    var fromCountry = $('#from-country-input').val(itemsInCart[i].FromCountry);
    var fromCity = $('#from-city-input').val(itemsInCart[i].FromCity);
    var fromPostCode = $('#from-postalcode-input').val(itemsInCart[i].FromPostCode);
    var fromPhone = $('#from-phone-input').val(itemsInCart[i].FromPhone);

    // to inputs
     var toName = $('#to-name-input').val(itemsInCart[i].ToName);
     var toLastName = $('#to-last-name-input').val(itemsInCart[i].ToLastName);
     var toAddress = $('#to-address-input').val(itemsInCart[i].ToAddress);
     var toCountry = $('#to-country-input').val(itemsInCart[i].ToCountry);
     var toCity = $('#to-city-input').val(itemsInCart[i].ToCity);
     var toPostCode = $('#to-postalcode-input').val(itemsInCart[i].ToPostCode);
     var toPhone = $('#to-phone-input').val(itemsInCart[i].ToPhone);
    break;
    }
  }

});

This is the form that is filled with the code above:这是用上面的代码填写的表格: 在此处输入图像描述

And then I want to use hit the "edit message" button in the form to update the new values that i changed in the form.然后我想使用点击表单中的“编辑消息”按钮来更新我在表单中更改的新值。

If I understand correctly.如果我理解正确。 You want to update sessionStorage item with new one from the form.您想用表单中的新项目更新 sessionStorage 项目。 Yes?是的? I would consider change the code a little bit to:我会考虑将代码更改为:

const tempObject = {
  FromName: $('#from-name-input').val(itemsInCart[i].FromName),
  FromLastName = $('#from-last-name-input').val(itemsInCart[i].FromLastName)
}
Object.assign(itemsInCart, tempObject)
sessionStorage.setItem('productList', JSON.strigify(itemsInCart));

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

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