我该怎么做对象数组

时间:2016-05-03 09:38:26

标签: javascript arrays object html-table

我有输入的html表。我想在inputbox中获得价值

<table id="yeni">
  <tr id="1">
    <td>
      <input id="1_1" style="width: 70px; font-size: 11px;">
    </td>
    <td>
      <input id="1_2" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="1_3" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="1_4" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="1_5" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="1_6" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="1_7" style="width: 80px; font-size: 11px;">
    </td>
  </tr>
  <tr id="2">
    <td>
      <input id="2_1" style="width: 70px; font-size: 11px;">
    </td>
    <td>
      <input id="2_2" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="2_3" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="2_4" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="2_5" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="2_6" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="2_7" style="width: 80px; font-size: 11px;">
    </td>
  </tr>
  <tr id="3">
    <td>
      <input id="3_1" style="width: 70px; font-size: 11px;">
    </td>
    <td>
      <input id="3_2" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="3_3" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="3_4" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="3_5" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="3_6" style="width: 80px; font-size: 11px;">
    </td>
    <td>
      <input id="3_7" style="width: 80px; font-size: 11px;">
    </td>
  </tr>
</table>

对于每一行ı想要创建对象

var stoktbl = {}
var table = [];

var rowCount = document.getElementById('yeni').rows.length;
for (var satirx = 1; satirx < rowCount; satirx++) {

  stoktbl.firma_barkod = document.getElementById(satirx + "_" + "1").value;
  stoktbl.adet = document.getElementById(satirx + "_" + "5").value;
  stoktbl.birim_satis_fiyati = document.getElementById(satirx + "_" + "6").value;

  table[satirx] = stoktbl;
}

var JsonString = JSON.stringify(table)
console.log(JsonString)

用它运行时;

我只看到像

那样的最后一行数据
[null, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}, {
  "adet": "1",
  "birim_satis_fiyati": "69.9",
  "firma_barkod": "091986 "
}]

1 个答案:

答案 0 :(得分:1)

在for-loop中,您需要重新初始化 stoktbl对象

for(var satirx=1; satirx<rowCount; satirx++){
   stoktbl = {};
   //rest of the cpde
}

否则,您的table数组将继续在所有索引处使用相同的对象。

此外,您需要使用push方法将对象添加到数组

for(var satirx=1; satirx<rowCount; satirx++)
{
    stoktbl = {};
    stoktbl.firma_barkod=document.getElementById(satirx +"_"+"1").value;
    stoktbl.adet= document.getElementById(satirx +"_"+"5").value;
    stoktbl.birim_satis_fiyati= document.getElementById(satirx +"_"+"6").value;
    table.push( stoktbl );
}

这将确保table数组的第一个元素不为空。