简体   繁体   English

如何从 jquery 的序列化字符串中删除空格?

[英]How to remove spaces from a serialized string for jquery?

var queryString = $('#formname').serialize();
$.post(queryString, function(data) { ...

Request URL sometimes becomes请求 URL 有时会变成

http:\\domain.com\\page.php?parm=1&contr y=2&... http:\\domain.com\\page.php?parm=1&contr y=2&...

how to remove the space in the text of country?如何去除国家文本中的空格? I had tried我试过了

 queryString.replace(/\s/g, '')

and

.trim 

but no success..但没有成功..

Thanks,谢谢,

replace works fine. replace工作正常。 Just strings are immutable , so you have to store or use directly the result of replace because it won't update your variable .只是字符串是不可变的,因此您必须直接存储或使用replace的结果,因为它不会更新您的变量

Quick note: this is kind of weird to have backslashes unescaped in your URL, but... eh, that's yours.快速说明:在您的 URL 中未转义反斜杠有点奇怪,但是......呃,那是你的。

 const url = 'http:\\domain.com\\page.php?parm=1&contr y=2&...'; console.log(url.replace(/\\s/g, '')); console.log(url);

By the way, trim only removes outer spaces.顺便说一下, trim只会删除外部空间。

 const str = ' Hello World! '; console.log(str); console.log(str.trim());

我相信使用trim()可以,trim()去掉不必要的空格

yourstring.trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM