如何使用JavaScript或Jquery从td输入中获取employee的值?

3mpgtkmj  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(122)

我想使用js或jquery获取输入字段的值,该字段位于表的以下tr中-

<tr>
   <td class="center"> <div class="hide"> <?php echo $row['id']; ?> </div> </td>
   <td class="center"> <div class="hide"> <?php echo $row['employee_no']; ?> </div> 
       <input type="tel" name="employee_no" id="employee_no" value="<?php echo 
       $row['employee_no']; ?>" onchange="return validate()" > </td>
</tr>

我试试这个密码-

function update(thisrow){
   var Row = document.getElementById(thisrow);
   var Cells = crw.getElementsByTagName("td");
   var id = Cells[0].innerText; 
   var employee = Cells[1].innerText;}

不幸的是,它给出了id的正确值,但给出了employee的空值?如何使用JS或Jquery从td输入中获得employee的值?

6l7fqoea

6l7fqoea1#

只需要获取元素并得到值。

function validate() {

  let t = document.getElementsByTagName("table")[0]
  let trs = t.getElementsByTagName("tr")

  for (i = 0; i < trs.length; i++) {
    let tds = trs[i].getElementsByTagName("td")
    for (j = 0; j < tds.length; j++) {
      let inp = tds[j].getElementsByTagName("input")
      if (inp.length > 0) console.log(inp[0].value)
    }
  }
}
<table>
  <tr>
    <td class="center">
      <div class="hide">
        <?php echo $row['id']; ?> </div>
    </td>
    <td class="center">
      <div class="hide">
        <?php echo $row['employee_no']; ?> </div>
      <input type="tel" name="employee_no" id="employee_no" value="000-123-1234" onchange="validate()"> </td>
  </tr>
</table>

相关问题