How can I get the value of an input in a specific table cell using javascript?

后端 未结 4 768
暖寄归人
暖寄归人 2020-12-18 06:43

I\'m wondering how can I get the value of an input in a specific table cell using javascript?


         


        
相关标签:
4条回答
  • 2020-12-18 07:08

    If you don't have any id on the element you are after, then you could get the first child of the td by:

    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;
    
    0 讨论(0)
  • 2020-12-18 07:09

    You could use firstChild.value like this:

    var x = document.getElementById("tabela").rows[2].cells[3].firstChild.value;
    

    Demo

    0 讨论(0)
  • 2020-12-18 07:15

    If you can provide id to your input element,

    HTML

    <td><input type="text" id="text1"/></td>
    

    JS

    var x = document.getElementById("text1").value;
    
    0 讨论(0)
  • 2020-12-18 07:15

    You can use querySelector() DOM method:

    document.querySelector('#tabela tr:nth-child(2) td:nth-child(3) > input').value
    

    JSFiddle

    0 讨论(0)
提交回复
热议问题