Javascript: Convert textarea into an array

江枫思渺然 提交于 2019-11-26 18:13:20

问题


How would you go about breaking up a textarea value into an array, based on the end of line separation? Use of jQuery is cool by me...


回答1:


This should work (tested in Firefox and Google Chrome):

var arrayOfLines = $('#textAreaID').val().split('\n');



回答2:


var stringArray = document.getElementById('textarea').value.split('\n');



回答3:


Cross-platform way:

var area = document.getElementById("area");             
var lines = area.value.replace(/\r\n/g,"\n").split("\n");



回答4:


You could try this function :

function textToArray(){
  var someArray = [];    
  var nameList = $("#txtArea").val();

  $.each(nameList.split(/\n/), function (i, name) {     

      // empty string check
      if(name != ""){

          someArray.push(name);

      }        
});

taken from : CONVERT TEXTAREA CONTENT TO AN ARRAY USING JQUERY




回答5:


This method worked well:

var textArea = document.getElementById("textAreaId");
var arrayFromTextArea = textArea.value.split(String.fromCharCode(10));


来源:https://stackoverflow.com/questions/2299604/javascript-convert-textarea-into-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!