Home  >  Article  >  Backend Development  >  javascript - html的textarea,提交的时候怎么去除首尾的空格?

javascript - html的textarea,提交的时候怎么去除首尾的空格?

WBOY
WBOYOriginal
2016-06-06 20:10:421128browse

html的textarea,提交的时候怎么去除首尾的空格?
前端js和后端php应该都可以去除吧?
怎么做呢?
需要说明的是,只去除整个textarea最开始和最后的空格,假如一段文字中间有换行,或有空格,不管它。

回复内容:

html的textarea,提交的时候怎么去除首尾的空格?
前端js和后端php应该都可以去除吧?
怎么做呢?
需要说明的是,只去除整个textarea最开始和最后的空格,假如一段文字中间有换行,或有空格,不管它。

首先,楼上很多人都说trim()了。

那么,textarea为什么首尾会有空格呢。。

不要这样:

<code><textarea rows="3" cols="20">
内容。
</textarea>

</code>

要这样:

<code><textarea rows="3" cols="20">内容</textarea></code>

使用字符串的string.trim()方法

trim()

都可以,在js中,一切皆对象,字符串也是,所以可以使用 字符串.trim()
在php中trim是一个函数,用法是trim(字符串)

可以用正则替换。但是用trim方便

var re = /^s+|s+$/g;
var str = $('#textarea').val();
var text = str.replace(re, '');

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn