繁体   English   中英

更新存储在会话存储中的多维数组

[英]updating a multidimensional array stored in session storage

我已经接手了一个项目,对JQuery和会话存储并不完全熟悉。 问题在于,更新信息并单击更新后,它会提供一个错误,“未捕获的SyntaxError:在JSON中意外at JSON.parse (<anonymous>) at fillArray (main.js? [sm]:313) at UpdateProduct (main.js? [sm]:234) at HTMLButtonElement.onclick ((index):405)".令牌u在at JSON.parse (<anonymous>) at fillArray (main.js? [sm]:313) at UpdateProduct (main.js? [sm]:234) at HTMLButtonElement.onclick ((index):405)".位置0 at JSON.parse (<anonymous>) at fillArray (main.js? [sm]:313) at UpdateProduct (main.js? [sm]:234) at HTMLButtonElement.onclick ((index):405)".

包含到github回购的链接: https : //github.com/bveasey/Jquery-project

function UpdateProduct() // called on click of an update btn
{

   // capture id from hidden element set in editproduct function
   var productId = $('#txtHiddenId').val();
   console.log("***** line:233 *****\n product id =" + productId);
   fillArray(productId);
   console.log("***** line:235 *****\n Fill array called!");
   BindTable();
   console.log("***** line:237 *****\n Bind Table called!");
}

// other functions 
function fillArray(prodid)
{
  console.log("***** line:310 *****\n  Begin Fill Array");

  // find the item in the product array and update it
  $.each(JSON.parse(sessionStorage.setItem('products', 
  JSON.stringify(productArray))), function (idx, v) 
{
  console.log("***** line:315 *****\n " + product.id);
  // Check if current prodid is the id wanted
  if (product.id === prodid) 
  {// ids match
    console.log("***** line:319 *****\n product is's match");

    // populate data
    product.id = productId;
    product.arrivalDate = $('#modalRequestedArrivalDate').val();
    product.productCode = $('#modalProductCode').val();
    product.description = $('#modalDescription').val();
    product.quantity = $('#modalQuantity').val();
    product.quantityType = $('#modalQuantityType').val();
    product.plant = $('#modalPlant').val();
    product.shippingMethod = $('#modalShippingMethod').val();
    product.specialInstructions = $('#modalSpecialInstructions').val();

    console.log("***** line:310 *****\n  END Fill Array");
  }
});

} //绑定表只会更新表。

Storage#setItem()返回void ,您不能使用JSON.parse()解析voidvoid JSON语法。

错误会准确告诉您有问题的代码在哪里,

 $.each(JSON.parse(sessionStorage.setItem('products', JSON.stringify(productArray))), function (idx, v)...

这里的sessionStorage.setItem()不会返回JSON.parse()可以解析的任何内容。

暂无
暂无

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

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