我想使用javascript获取表格单元格的id。我试过上面提到的
alert(tableId.rows[i].cells[5].firstChild.id);
这很好用:
alert(tableId.rows[i].cells[5].firstChild.value);
但是如何获取元素的id:
表:
<table border="1px solid" style="width:100%;text-align: center;" class="item_table" id="item_grid">
<thead>
<tr>
<th>Type</th>
<th>Item</th>
<th>Gross wt.</th>
<th>Ded.</th>
<th>Purity</th>
<th>Remark</th>
</tr>
</thead>
<tbody>
{% for subform in item_form %}
<tr class="inline {{ item_form.prefix }}">
{{ subform.non_field_errors }}
{{ subform.type.errors }}
<td>{{ subform.type }}</td>
{{ subform.name.errors }}
<td>{{ subform.name }}</td>
{{ subform.weight.errors }}
<td>{{ subform.weight }}</td>
{{ subform.deduction.errors }}
<td>{{ subform.deduction }}</td>
{{ subform.purity_percent.errors }}
<td>{{ subform.purity_percent }}</td>
{{ subform.fine_weight.errors }}
<td style="visibility: hidden;display: none;">
{{ subform.fine_weight }}
</td>
{{ subform.remark.errors }}
<td>{{ subform.remark }}</td>
</tr>
{% endfor %}
</tbody>
{{ item_form.management_form }}
</table>
渲染表体:
<tbody>
<tr class="inline item_set dynamic-form">
<td>
<select id="id_item_set-0-type" name="item_set-0-type" required="required">
<option value="">---------</option>
<option value="1" selected="selected">GOLD</option>
<option value="2">SILVER</option>
</select></td>
<td>
<select id="id_item_set-0-name" name="item_set-0-name" required="required">
<option value="">---------</option>
<option value="13" selected="selected">ankit</option>
<option value="6">bangle</option>
<option value="1">chain</option>
<option value="3">churi</option>
<option value="10">haha</option>
<option value="12">huha</option>
<option value="7">lengha</option>
<option value="4">lungi</option>
<option value="9">new test</option>
<option value="8">test</option>
<option value="11">test item</option>
</select>
<a href="/add/item_name/popup/" class="add-another" id="add_id_item_set-0-name" onclick="return showAddAnotherPopup(this);">
<img src="/static/girvi/images/icon_addlink.gif" alt="Add Another" height="10" width="10">
</a>
</td>
<td>
<input id="id_item_set-0-weight" maxlength="6" name="item_set-0-weight" onkeypress="return isNumberKey(event,"id_item_set-0-weight")" onkeyup="return isNumberKey(event,"id_item_set-0-weight")" required="required" size="12" value="100" type="text">
</td>
<td>
<input class="per" id="id_item_set-0-deduction" maxlength="6" name="item_set-0-deduction" onkeypress="return isNumberKey(event,"id_item_set-0-deduction")" onkeyup="return isNumberKey(event,"id_item_set-0-deduction")" required="true" size="10" value="10" type="text">
</td>
<td>
<input class="per" id="id_item_set-0-purity_percent" maxlength="3" name="item_set-0-purity_percent" onkeypress="return isNumberKey_purity(event)" onkeyup="return isNumberKey_purity(event)" required="required" size="10" value="80" type="text">
</td>
<td style="visibility: hidden;display: none;">
<input id="id_item_set-0-fine_weight" maxlength="6" name="item_set-0-fine_weight" required="true" size="10" value="" type="hidden">
</td>
<td>
<textarea cols="40" id="id_item_set-0-remark" maxlength="150" name="item_set-0-remark" placeholder="OK" rows="10">
OK
</textarea>
<a style="display: none;" class="delete-row" href="javascript:void(0)">
del
</a>
</td>
</tr>
<tr>
<td colspan="7">
<a class="add-row" href="javascript:void(0)">
Add
</a>
</td>
</tr>
</tbody>