问题
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