【发布时间】:2012-01-18 17:26:18
【问题描述】:
我有一个textarea,用户可以在其中写入最多 1000 个字符。我需要获取jQuery('#textarea').val() 并创建一个数组,其中每个项目都是textarea 值的一行。这意味着:
这是文本区域内的一条很好的线。
这是另一行。
(让我们假设这一行是空的 - 它应该被忽略)。
有人在上面留下了超过 2 个新行。
应转换为 JavaScript 数组:
var texts = [];
text[0] = 'This is a nice line inside the textarea.';
text[1] = 'This is another line.';
text[2] = 'Someone left more than 2 new lines above.';
这样他们就可以轻松imploded查询字符串(这是提供者要求的qs格式):
example.com/process.php?q=["This is a nice line inside the textarea.","This is another line.","Someone left more than 2 new lines above."]
我尝试了phpjs explode() 和string.split("\n") 两种方法,但它们没有处理额外的新行(也就是换行符)。有什么想法吗?
【问题讨论】:
-
就在我的脑海中,也许尝试拆分它,然后将拆分的位输入一个数组?
-
@AndrewPeacock 我刚刚用一些信息更新了帖子。我用 phpjs explode() 试过了,但它没有处理额外的行(如果有的话)问题。
标签: javascript jquery arrays string newline