Javascript获取表中输入的值 [英] Javascript getting the value of an input in a table
本文介绍了Javascript获取表中输入的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道是否有可能在表中获取html输入的值而不单独命名每个输入并直接在输入上使用getElementById,所以如果我有下表
I'm just wondering if it's possible to get the value of an html input in a table without naming each input separately and using getElementById directly onto the input so if I had the following table
<table id="table01">
<tr>
<td>row 0 cell 0</td>
<td>row 0 cell 1</td>
</tr>
<tr>
<td>row 1 cell 0</td>
<td>row 1 cell 1</td>
</tr>
</table>
我知道在Javascript中你可以使用以下内容来获取特定行中特定单元格的值使用以下
I know in Javascript you can use the following to get the value of a specific cell in a specific row using the following
var lv_value = document.getElementById("table01").rows[0].cells[1].innerHTML;
console.log(lv_cont);
这会给我一个我想要的值,即row 0 cell 1。
and this would give me the value I want which is "row 0 cell 1".
如果我有如下表格,那么
If I had a table like the following however
<table id="table01">
<tr>
<td>row 0 cell 0</td>
<td>row 0 cell 1</td>
<td><input type="text" class="tbl_input"></input></td>
</tr>
<tr>
<td>row 1 cell 0</td>
<td>row 1 cell 1</td>
<td><input type="text" class="tbl_input"></input></td>
</tr>
</table>
是否可以按照
<!-- this is obviously wrong -->
var lv_input = document.getElementById("table01").rows[0].cells[2].input.value;
console.log(lv_input);
获取第一行输入的值
推荐答案
你应该这样做:
var lv_input = document.getElementById("table01").rows[0].cells[2].firstChild.value;
console.log(lv_input);
或使用 querySelector
查找输入元素
var lv_input = document.getElementById("table01").rows[0].cells[2].querySelector('input').value;
console.log(lv_input);
这篇关于Javascript获取表中输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文