繁体   English   中英

如何将textarea内容推入数组

[英]How do I push textarea content into an array

我有一个用户可以写入的textarea。可以将其压入数组吗? 当我console.log时,我得到“ [object HTMLTextAreaElement]”-这可以做吗?

<textarea id="sermon" cols="100" rows="20">
Write here...
</textarea>

</div> <button id="newContent"><a href='#' onclick='downloadCSV({ filename: "card-data.csv" });'>Download your text to a CSV.</a></button>


var myArray = [];
myArray.push(sermon);
console.log(myArray.join());

尝试使用toString()函数,如下所示:

Write here...
</textarea>

</div> <button id="newContent"><a href='#' onclick='downloadCSV({ filename: "card-data.csv" });'>Download your text to a CSV.</a></button>


var myArray = [];
myArray.push(sermon.toString());
console.log(myArray.join());

HTML应该是这样的:

<textarea id="sermon" cols="100" rows="20">

</textarea>

<button onclick="pushData()">Add to Array</button>

JavaScript应该是这样的:

function pushData(){
   var text= document.getElementById("sermon").value;

    var array = [];

    array.push(text); 

    console.log(array.toString());
}

您可能会发现不调用javascript内联,而是将某些操作绑定到click事件很方便。

HTML

<textarea id="sermon" cols="100" rows="20">
Write here...
</textarea>

<button id="newContent">Download your text to a CSV.</button>

Java脚本

// wait for your page to be loaded
window.onload = function() {
  // declare your empty variables
  var myArray = [];
  var sermon;

  // find the button element
  var button = document.getElementById('newContent');

  // attach some code to execute on click event
  button.onclick = function() {
    // put a call to your download function here

    // get the new value of the text area
    sermon = document.getElementById('sermon').value;

    myArray.push(sermon);
    console.log(myArray.join());
}  
};

暂无
暂无

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

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