简体   繁体   English

如何在jQuery中使用$ .param

[英]how to use the $.param in jquery

I have searched the net already and the only useful information is in the jquery site itself, 我已经在网上搜索过,唯一有用的信息是在jquery网站本身中,

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: 发生这种情况是因为您的branch_id在结尾处包含一个空格:

"1234 " // => 1234+

Take a look at source code of jQuery.param . 看一下jQuery.param代码

So the space is first converted to "%20" using encodeURIComponent and after that jQuery.param replaces is with a plus sign: 因此,首先使用encodeURIComponent将空间转换为"%20" ,然后将jQuery.param替换为加号:

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

You can trim input value to avoid this redundant + or maybe cast value to a number. 您可以调整输入值,以避免出现多余的+或将值强制转换为数字。

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

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