0

我有一个 5x5 的 HTML 表格,即每个 tr 有 5 个 td,每个 td 有一个输入字段,我想解析它的值,每个 td 都有数据行和数据列的数据属性。这是我想出的,但它有问题,我该怎么做?

tds = $('td')
marker = 0
thisSet = []
table = []

for td in tds
 thisRow = parseInt($(td).attr('data-row'))

 if marker == thisRow
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"
  thisSet.push ({data: rc})
  console.log "marker:#{marker}, thisRow:#{thisRow}"
else
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"
  thisSet.push ({data: rc})
  marker = thisRow 
  console.log "marker:#{marker}, thisRow:#{thisRow}"
  table.push thisSet
  thisSet = []

console.log table
console.log _.flatten(table).length

更新:好的,在它上面工作了一点,现在我解析了 4 行,而不是第 5 行,缺少一些东西,但是 4 行解析得很好。

tds = $('td')
currentRow = 0
thisSet = []
table = []
for td in tds
  thisRow = parseInt($(td).attr('data-row'))
  rc = "#{$(td).attr('data-row')}-#{$(td).attr('data-column')}"

 if currentRow != thisRow
  table.push thisSet
  thisSet = []
  thisSet.push ({data: rc})
  currentRow = thisRow 
else
  thisSet.push ({data: rc})

console.log table
console.log _.flatten(table).length
4

1 回答 1

1

我可能会这样做:

table = []
table.push([]) for num in [0...5]

tds = $('td')

for td in tds
  row = parseInt(td.attr(data-row))
  col = parseInt(td.attr(data-column))

  table[row][col] = { data: "#{row}-#{col}" }

console.log table
于 2013-11-03T07:08:21.870 回答