简体   繁体   English

如何使用 JS 获取这个 HTML Table textarea 的值?

[英]How to get this HTML Table textarea's value using JS?

I've tried some suggestions, but no success so far.我尝试了一些建议,但到目前为止没有成功。

I'm getting null when trying to get this textarea value while going through the table and I don't know exactly why:在浏览表格时尝试获取此textarea值时,我得到null ,但我不知道确切原因:

function savePo(user, origin) {
  user = 'user';
  origin = 'Trim';


  let date = new Date();
  options = {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
    hour12: false,
    timeZone: 'CST'
  };

  let timeStamp = new Intl.DateTimeFormat('en-US', options).format(date).toString();
  timeStamp = timeStamp.replace(",", "");

  const tableRows = document.querySelectorAll("#tableRows tr");

  let tableData = [];
  if (origin == 'Trim') {
    tableData = [...tableRows].map(r => {
      let td = r.querySelectorAll("td");
      return [...td].map((c, j) =>
        j == 2 ? c.value : //THIS IS THE LINE WHICH IS NOT WORKING PROPERLY
        j == 7 ? c.querySelectorAll('input[type="checkbox"]')[0].checked :
        j == 6 ? c.innerText :
        c.querySelector('input').value)
    });
    console.log('Table Data: ' + tableData)
  }
}

Here is the Fiddle in case you feel like pointing where the flaw is. 这是 Fiddle ,以防您想指出缺陷所在。

Appreciate it!欣赏它!

To get the value of a textarea element, instead of innerText of the parent element use value of such textarea element.要获取 textarea 元素的值,而不是父元素的innerText使用此类 textarea 元素的value

 const text = document.querySelector('td textarea').value console.log(text)
 <table> <tr> <td> <textarea name="articuloField" id="articulo" rows="2" cols="65" wrap="soft">Testing the textarea value field which never seems to work!!!!!!!!!!</textarea> </td> </tr> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM