简体   繁体   中英

how to use the $.param in jquery

I have searched the net already and the only useful information is in the jquery site itself,

I have this code:

var param = { branch_id : branch_id};
var str = $.param(param);
alert(str);

but the output of that when logged or alerted is:

branch_id=1234 + //lets say for example 1234 is the value.

Why is there a plus sign there? that is my question.

It happens because your branch_id contains a space at the end:

"1234 " // => 1234+

Take a look at source code of jQuery.param .

So the space is first converted to "%20" using encodeURIComponent and after that jQuery.param replaces is with a plus sign:

return s.join("&").replace(r20, "+");

You can trim input value to avoid this redundant + or maybe cast value to a number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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