我正在使用form.serialize()
来获取参数列表。
如果参数为空或其空格,我想将其从列表中删除。
例如:
testAction.action?a=1&b=&c=3
应该给我
testAction.action?a=1&c=3
首先我使用正则表达式:
params = params.replace(/[^&]+=\.?(?:&|$)/g, '');
但问题是,如果我的网址是
testAction.action?a=1&b=2&c=
正则表达式会回复我
testAction.action?a=1&b=2& (i have & at the end!)
之后我尝试了jQuery解决方案
$('.myForm').find('input, select').not("[value='']").serialize();
但这只适用于空值 - >如果我在通过的值中有空格参数。
你能帮我解决一些其他问题吗?
由于
答案 0 :(得分:1)
尝试
$(".myForm :input").filter(function () {return $.trim(this.value);}).serialize();
答案 1 :(得分:0)
你可以这样做:
params = params.replace(/&?[^&?]+=(?=(?:&|$))/g, '');