Javascript 如何获取放置在行单元格中的文本框的值

Javascript 如何获取放置在行单元格中的文本框的值,javascript,html,Javascript,Html,我正在遍历具有以下结构的表 <tr val='question'> <td ><input style='width:500px' type='text' placeholder='Q.Enter your question here for radio button?'> </tr> 我想使用Javascript检索输入框的值,将name和id属性添加到输入元素中 <tr> <td ><inpu

我正在遍历具有以下结构的表

<tr val='question'>
    <td ><input style='width:500px' type='text' placeholder='Q.Enter your question here for radio button?'>
</tr>


我想使用Javascript检索输入框的值,将
name
id
属性添加到输入元素中

<tr>
    <td ><input style='width:500px' type='text' id='ab' name='ab' placeholder='Q.Enter your question here for radio button?'>
</tr>
var-value=document.querySelectorAll('input[type=text]')。值;
var-arr=[];
对于(i=0;i
通过这种方式,您将得到一个数组,其中包含输入的所有值,类型为text

  • 给你的
    一个
    id
    属性

  • theValue=document.getElementById('yourId').value


  • 就是这样。

    这是一个糟糕的解决方案,因为它将匹配页面上的第一个文本输入并获得值。它在有多个文本输入的任何页面上都不起作用。querySelector本身可以很好,但最好将元素作为特定容器的子元素查找。或者给元素一个ID(如果结构允许的话)并以这种方式选择它。现在更好吗?
    <script>
    var qn = document.getElementById("ab").value;
    </script>
    
    var value = document.querySelectorAll('input[type=text]').value;
    var arr = [];
    for(i = 0; i < value.length; i++)
        arr[i] = value[i];