Serialize method for jquery How to convert a space to + number solution

Source: Internet
Author: User

jquery's Serialize () method, which can serialize a form item, is a handy feature, but in practice it finds the following problems:
For example:
< textarea name= "content" >ABC 123 </textarea >
then after executing the serialize () method, the resulting string is abc+123;
that is, the serialization method of jquery escapes the space and translates it into a + sign.
part of the code for the Serialize method in jquery is as follows:
//.................
//.................
return S.join ("&"). Replace (//g, "+"); Replace the space with the + sign

after testing, the Serialize () method escapes the true "+" number by%2b, so you can rest assured that the result of serialize () is replaced with a symbol.
Example:

< textarea name= "content" >ABC + 123 + 456 </textarea >

jquery Code:
var a= $ (' $frm 1 '). Serialize ();//serialization, by default calls encodeURIComponent () to encode
alert (a);//content=abc+++123+++456
var b = a.replace (/\+/g, ""); G means to replace all eligible in the entire string
B = decodeuricomponent (b); Decode the content after the serialize
alert (b);//CONTENT=ABC + 123 + 456
at this point, the problem is solved.

Serialize method for jquery How to convert a space to + number solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.