javascript - 如何获取表 TR 上的所有输入值(按 ID)
问题描述
你好吗?
如何获取表中一个 TR 内的所有输入值?我会找到示例,而不是针对特定行(带有 ID)我有这个表:
<table>
<tr id="a">
<td><input id="a_01" value="the value of a_01"></td>
<td><input id="a_02" value="the value of a_02"></td>
</tr>
<tr id="b">
<td><input id="b_01" value="the value of b_01"></td>
<td><input id="b_02" value="the value of b_02"></td>
</tr>
</table>
例如:我正在尝试获取 id="b" 的 tr 的所有值输入。
非常感谢您的帮助!
解决方案
您可以使用选择器来定位元素。为此,您不需要 jQuery,因为您可以使用querySelector
andquerySelectorAll
函数:
document.querySelector('button').addEventListener('click', function(){
// Radio input value
var value = document.querySelector('input[name="idvalue"]:checked').value;
// Here's the selector for the input elements
var elements = document.querySelectorAll('#' + value + ' input');
// You can iterate the result and use the element values
elements.forEach(e => {console.log(e.id + ': ' + e.value);});
});
<table>
<tr id="a">
<td><input id="a_01" value="the value of a_01"></td>
<td><input id="a_02" value="the value of a_02"></td>
</tr>
<tr id="b">
<td><input id="b_01" value="the value of b_01"></td>
<td><input id="b_02" value="the value of b_02"></td>
</tr>
</table>
<div>
<input type="radio" name="idvalue" id="radioa" value="a" checked /><label for="radioa">Show values for #a</label>
<input type="radio" name="idvalue" id="radiob" value="b" /><label for="radiob">Show values for #b</label>
</div>
<button>Show Values of selected #id</button>
如果您真的 不需要在jquery中执行此操作,则可以使用相同的选择器来定位元素:$("#b input")
.
推荐阅读
- angular - Angular Proxy 绝对 URL 问题
- javascript - 如何访问在 ReactNative 中呈现 AppContainer 的类中的 navigation/goBack 函数
- java - 如何防止执行jsp scriptlet?
- trailing-slash - 如何仅在目录末尾添加斜杠
- swift - 快速实时相机中的文本检测
- python - pd.read_csv 读取的行数少于预期
- java - 如何使用 Java API 更正访问 Hadoop 3.2.1?
- python - 返回参数列表时的奇怪输出
- excel - EXCEL.EXE 中来自 Windows 服务的 Excel 互操作崩溃,版本:16.0.4966.1000,模块:oart.dll,版本:16.0.4900.1000
- python - Python用一个公共列连接两个框架