javascript 如何使用javascript获取特定表格单元格中的输入值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24307292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the value of an input in a specific table cell using javascript?
提问by Casi
I'm wondering how can I get the value of an inputin a specific table cell using javascript?
我想知道如何input使用 javascript获取特定表格单元格中的值?
<td><input type="text"/></td>
I assume getting the innerHTMLof a specific cell is quite simple, for example:
我假设获取innerHTML特定单元格的 非常简单,例如:
var x = document.getElementById("tabela").rows[2].cells[3].innerHTML
but this gives me just the inputwithout it's value. Adding .valueto the end of the line doesn't work. I would appreciate your help!
但这给了我input没有它的value。添加.value到行尾不起作用。我将不胜感激您的帮助!
回答by Abhitalks
If you don't have any idon the element you are after, then you could get the first child of the tdby:
如果您id在追求的元素上没有任何元素,那么您可以通过以下方式获取第一个子元素td:
var x = document.getElementById("tabela").rows[n].cells[n].children[0].value;
Or if you want the first child to be specific to input then:
或者,如果您希望第一个孩子特定于输入,则:
var x = document.getElementById("tabela").rows[n].cells[n].getElementsByTagName('input')[0].value;
回答by naota
回答by Mustafa sabir
If you can provide id to your input element,
如果您可以为输入元素提供 id,
HTML
HTML
<td><input type="text" id="text1"/></td>
JS
JS
var x = document.getElementById("text1").value;
回答by potashin
You can use querySelector()DOM method:
您可以使用querySelector()DOM 方法:
document.querySelector('#tabela tr:nth-child(2) td:nth-child(3) > input').value

