jquery:如何遍历 textarea 中键入的每个换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1726747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:02:50  来源:igfitidea点击:

jquery: how do you loop through each newline of text typed inside a textarea?

jquery

提问by aoghq

lets say inside a <textarea>, i type in a bunch of keywords on each new line.

假设在 a 中<textarea>,我在每一行中输入一堆关键字。

keyword1
keyword2
keyword3
...




$('textarea[name=sometextarea]').val().split('\n').each(function(e){
alert($(this));                 
});

回答by Guffa

The array object doesn't have any eachmethod. As you are looping strings and not elements, use the jQuery.eachmethod (instead of the jQuery().each method):

数组对象没有任何each方法。当您循环字符串而不是元素时,请使用该jQuery.each方法(而不是 jQuery().each 方法):

var lines = $('textarea[name=sometextarea]').val().split('\n');
$.each(lines, function(){
  alert(this);
});

回答by fserb

I think your problem is with the jquery object. val() returns a string and split() will return an Array. But each() is a property of the JQuery object. Try this instead:

我认为您的问题出在 jquery 对象上。val() 返回一个字符串, split() 将返回一个数组。但是 each() 是 JQuery 对象的一个​​属性。试试这个:

$($('textarea[name=sometextarea]').val().split('\n')).each(function(e){
alert($(this));                                 
});

notice the extra $(...) around the the split() return.

注意 split() 返回周围的额外 $(...) 。

回答by Rudd Zwolinski

jQuery provides an eachmethod on the jQuery(or $) object. This should be used, rather than creating a jQuery object out of an array:

jQueryeachjQuery(or $) 对象上提供了一个方法。应该使用它,而不是从数组中创建一个 jQuery 对象:

$.each($('textarea[name=sometextarea]').val().split('\n'), function(e){
    alert(this);
});

回答by Hussein Ahmed

$("#your-text-area").live("keypress",function(){
            var lines = $(this).val().split("\n");
            $.each(lines, function(n, elem) {
                     console.log(elem);
            $("<li></li>").text(elem).appendTo($("#target"));
});

You can "listen" using live on keypress, as the others suggested split by newline and the trick in the loop is that with "each" you get an index and value (elem in the code). This snippet appends the line value to another target element which you can empty before looping using $("#target").empty();

您可以在按键上使用实时“收听”,因为其他人建议按换行符拆分,循环中的技巧是使用“每个”可以获得索引和值(代码中的元素)。此代码段将行值附加到另一个目标元素,您可以在使用 $("#target").empty(); 循环之前将其清空。

回答by pinarella

If i have a textarea that has some width, and text typed, where text automaticly goes to new line because of the end of textarea, can that string be split into new string per line?

如果我有一个 textarea 有一定的宽度,并且输入了文本,由于 textarea 的结尾,文本会自动转到新行,该字符串可以拆分为每行的新字符串吗?

回答by Sverre ?lnes

Like this...

像这样...

$.each($('textarea[name=sometextarea]').val().split('\n'), function(e){
alert(this);                                 
});

回答by pinarella

I have solved this with codeigniter and php. Here is a code:

我已经用 codeigniter 和 php 解决了这个问题。这是一个代码:

$this->load->helper('text');
$name = 'some text that is longer than the width of text area and you need to do some thing with it...';
$name_array = word_wrap($name, 20); //20 is a number of character where your text will be wrapped.
foreach(preg_split("/((\r?\n)|(\r\n?))/", $name_array) as $line){
         echo $line.'</br>';
    //from here, you can do what ever you want with $line, put it to another array..whatever
}