【问题标题】:Generating a price per item in an html table using a javascript使用 javascript 在 html 表中生成每个项目的价格
【发布时间】:2017-06-28 05:27:35
【问题描述】:

我有一个 javascript 正在为我生成一个表格。表中的元素聚集在一个名为 sep 的数组中。 Sep 包含 1152 个子数组,格式如下:

Sep[0] //["316SS", "K", "-100 to 225°C", "Brass", "1/8", "4'", "4'", "8", "Ungrounded"]

所以基本上有 1152 行,每行定义一个产品,有 9 个参数。我想制作一个 for 循环,为每个配置创建一个价格。这是我目前所拥有的:

//PART 1-------------WORKS FINE-----------------------------------
  var eopartprice2 = []; //matrix that I want to contain my prices

  for (var i = 0; i < sep.length; i++) {
  strnum1 = sep[i][5]; //parameter 5 is a length of material
  len1 = Number(strnum1.substr(0, strnum1.length - 1));

  strnum2 = sep[i][6]; //parameter 6 is another length of material
  len2 = Number(strnum2.substr(0, strnum2.length - 1));

  strnum3 = sep[i][7]; //parameter 7 is the number of units required
  condnum = Number(strnum3.substr(0, strnum3.length));

  feetOfMat = len1*len2*condnum; //The product of these is the total feet of req material

//PART 2------------PFCost always = 0.87--------------------------  
//Next i need to identify the cost of the material (to multiply by the total feet)

  var costOfMat = [0.87, 0.87, 1.77, 0.55] //different costs of the 4 materials
  if (sep[i][0] = "304SS") {
   var PFCost = costOfMat[0]; //304SS costs 0.87/foot
  } else if (sep[i][0] = "316SS") {
   var PFCost = costOfMat[1]; //316SS costs 0.87/foot
  } else if (sep[i][0] = "Inconel") {
   var PFCost = costOfMat[2]; //Inconel costs 1.77/foot
  } else if (sep[i][0] = "High Temp. Glass") {
   var PFCost = costOfMat[3]; //High Temp. Glass costs 0.55/foot
  }

  baseMatCost[i] = PFCost*feetOfMat; //I'd like to generate a matrix that
  //contains all of the base prices (1 for each row)

//PART 3---------------fitcost always = 36------------------------
//Trying to identify the cost of brass vs. stainless fittings
  if (sep[i][3] = "Brass") {
   fitcost = 36;
  } else if (sep[i][3] = "Stainless Steel") {
   fitcost = 37;
  }
}

到目前为止,我的问题是我希望根据 if 语句是否满足来定义价格,但在这两种情况下(fitcost 和 PFCost),值只是第一个 if 语句中定义的值。

最后,我想在 eopartprice2 矩阵中生成我的最终价格,基于将上面生成的材料相加 + 一些劳动力成本乘以一些利润。

我还担心它的运行速度有多快,因为它将在我的网站中成为一个实时表格,每次我添加更多内容时,我都觉得生成它需要的时间越来越长。这是我正在工作的 w3 的link

请,任何帮助将不胜感激:)

【问题讨论】:

    标签: javascript if-statement for-loop html-table


    【解决方案1】:

    在您的 if 语句条件中,您使用的是单个等号。这是assignment operator,而不是comparison operator

    所以,if (sep[i][0] = "304SS") 这样的 if 语句实际上是在赋值“304SS”;它将值“304SS”与sep[i][0] 进行比较。

    要正确比较这些值,您需要将单等号更改为双等号:

    if (sep[i][0] == "304SS").

    注意:== 将在比较之前根据需要转换类型。例如:".87" == 0.87 返回true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 2016-12-28
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      相关资源
      最近更新 更多