我有一个 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