Jquery的序列化方法为什么要把空格替换成加号呢?

hansonboy hansonboy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

param: function( a ) {
var s = [ ];

function add( key, value ){
s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
};

// If an array was passed in, assume that it is an array
// of form elements
if ( jQuery.isArray(a) || a.jquery )
// Serialize the form elements
jQuery.each( a, function(){
add( this.name, this.value );
});

// Otherwise, assume that it's an object of key/value pairs
else
// Serialize the key/values
for ( var j in a )
// If the value is an array then the key names need to be repeated
if ( jQuery.isArray(a[j]) )
jQuery.each( a[j], function(){
add( j, this );
});
else
add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );

// Return the resulting serialization
return s.join("&").replace(/%20/g, "+");
}



为什么要替换下呢,为什么要替换成加号呢?return s.join("&").replace(/%20/g, "+");



如果用户在输入框中输入了空格的字符串;我用JQuery的序列化方法得到URL串然后提交的方式,就会造成后台数据的不正确,不知道有没有好的办法?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
hansonboy
JavaScript 87,983 成员 224,681 内容