簡體   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