【发布时间】:2013-02-04 07:29:54
【问题描述】:
我知道这不应该这么难,但我需要另一双眼睛,因为我正在用头撞墙。
我有一个 NESTED 表,其 ID 如下:
<table id="dwMeasurements_0">
<tbody>
<tr id="tDim_0">
<td colspan="2">
<strong>Total Wall Length: </strong>
</td>
<td>
<input type="text" id="Msr_0_T1" class="dwInput" value="0"> Inches and </td>
<td>
<input type="text" id="Msr_0_T2" class="dwInput" style="margin-left:2px;" value="0"> 16ths</td>
</tr>
<tr>
<td colspan="4">
<hr>
</td>
</tr>
<tr id="lDim_0_0">
<td>
<select id="ItemType_0_0">
<option>Item Type</option>
<option>Door</option>
<option>Window</option>
<option>Other</option>
</select>
</td>
<td>
<label>L-Dim: </label>
</td>
<td>
<input type="text" id="Msr_0_0_A1" class="dwInput" value="0"> Inches and </td>
<td>
<input type="text" id="Msr_0_0_A2" class="dwInput" style="margin-left:2px;" value="0"> 16ths</td>
</tr>
//MORE ROWS HERE//
</table>
我的jQuery序列化文本输入和选择元素如下:
var MeasureRowCount = $("[id^='MeasureRow_']").length; //Populated by counting parent rows
var htmlOutput = '';
var rowInputs,rowSelects;
for(var r=0;r < MeasureRowCount;r++){
rowInputs = $('#dwMeasurements_'+r+' :input').serializeArray();
rowSelects = $('#dwMeasurements_'+r).find('select').serializeArray();
$.each(rowSelects, function(i, eSelectItem){
esName = eSelectItem.name;
esVal = eSelectItem.value;
htmlOutput += //name and value from above with markup
}
}
// htmlOutput to DOM here with markup
我尝试了多种方法来收集输入元素,但都没有奏效。数组出现空。即使表是嵌套的,我直接调用嵌套表ID,它不应该工作吗?
【问题讨论】:
-
jsfiddle 和控制台消息请
-
使用像
[id^='MeasureRow_']这样的选择器是一个很好的指标,表明您应该改用公共类。 -
补充@Blazemonger 所说的,我看不出你在哪里使用
MeasureRow_something的任何ID,MeasureRowCount 的计数将为0 -
“MeasureRow_”是父
,用于简单地计算嵌套表所在循环的行数。除非需要调用子元素否则不重要? MeasureRow_是动态创建的,末尾有一个数字,所以创建时 id 都是唯一的。我没有在这里包含它,因为MeasureRow_的计数可以填充循环变量。
标签: jquery html-table serializearray