[英]Group values in querystring generated with .serialize
I have a form: 我有一个表格:
<form id="formFilter">
<input type="checkbox" name="type" id="type_red" value="red" ?>
<label for="type_red">Red</label>
<input type="checkbox" name="type" id="blue" value="Blue" ?>
<label for="type_blue">Blue</label>
</form>
Then, I am using this jQuery to serialize the fields: 然后,我使用此jQuery序列化字段:
fp("#formFilter").submit(function(){
var querystring = fp(this).serialize();
fp('#result').html(querystring);
return false;
});
This returns the string type=Red&type=Blue
这将返回字符串type=Red&type=Blue
Except that I need a string like this: type=Red,Blue
除了我需要这样的字符串: type=Red,Blue
I will appreciate if someone can point me in the right direction to accomplish this. 如果有人能指出我正确的方向来完成此任务,我将不胜感激。
Change the name to make them group automagically, from: 从以下位置更改名称以使其自动分组。
<input type="checkbox" name="type" id="type_red" value="red" ?>
to 至
<input type="checkbox" name="type[]" id="type_red" value="red" ?>
This will group them on the serverside. 这会将它们分组在服务器端。
If for some strange reason all you're trying to do is create a string, you could do: 如果出于某种奇怪的原因,您想要做的只是创建一个字符串,则可以执行以下操作:
$("#formFilter").on('submit', function(){
var qs = $(this).serializeArray(),
q = {},
querystring = "?";
$.each(qs, function(idx, arr) {
if (q[arr.name] === undefined) q[arr.name] = [];
q[arr.name].push(arr.value);
});
$.each(q, function(name, value) {
querystring += (name + "=" + value.join(',') + "&");
});
querystring = querystring.substring(0, querystring.length - 1);
$('#result').html(querystring);
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.