jQuery序列化将所有空格转换为加号 [英] jQuery serialize converts all spaces to plus

查看:317
本文介绍了jQuery序列化将所有空格转换为加号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,在所有我要使用序列化的地方,我都必须像这样使用它:

Currently, everywhere I use serialize I have to use it like this:

.serialize().replace(/\+/g,'%20');

否则,表单数据中的任何空格都将被加为+.是否有可以将其设置为默认值的设置.

otherwise any spaces in the form data will be coverted to +'s. Is there a setting that can make this the default.

推荐答案

有趣的是,这是一种不使用临时变量的替代方法:

For fun, here's an alternative that doesn't use a temporary variable:

$.fn.serializeAndEncode = function() {
    return $.map(this.serializeArray(), function(val) {
        return [val.name, encodeURIComponent(val.value)].join('=');
    }).join('&');
};

$("#formToSerialize").serializeAndEncode();

这篇关于jQuery序列化将所有空格转换为加号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆