如果我对名称中方括号的输入使用jQuery serialize(),为什么会出现这个分号?
I have the following HTML:
我有以下HTML:
<input type="checkbox" id="options_1" value="options_1" name="options[]">
<input type="checkbox" id="options_2" value="options_2" name="options[]">
<input type="checkbox" id="options_3" value="options_3" name="options[]">
I check the first two options and send it to the server via ajax in jQuery:
我检查了前两个选项,并通过jQuery ajax发送到服务器:
$.ajax({
type: "POST",
url: "myfile.php",
data: {
'options':$('input[name="options[]"]').serialize()
},
dataType: 'json',
beforeSend: function(){
//do some stuff
},
success: function(msg){
//do some stuff
}
});
Firebug shows me the data that has been posted:
Firebug向我显示已发布的数据:
options options%5B%5D=options_1&options%5B%5D=options_2
So far, so good.
In myfile.php I get the POST-Variable like this:
选项选项%5B%5D=options_1&选项%5B%5D=options_2,到目前为止,很好。在myfile。php后变量如下:
$options = $_POST['options'];
Now when I echo $options I get this:
当我回复$options时,我得到了这个:
"options[]=options_1&options;[]=options_2"
Where does this semicolon in front of the second pair of brackets come from? This is driving me crazy.
在第二对括号前面的分号来自哪里?这让我发疯。
I already used utf8_decode on the POST data as well as urldecode and rawurldecode. Nothing changes. I also escaped the square brackets in the ajax call like this:
我已经在POST数据上使用了utf8_decode以及urldecode和rawurldecode。没有什么变化。我还将ajax调用中的方括号转义为:
data: {
'options':$('input[name="options\\[\\]"]').serialize()
},
That didn't help either. Any ideas anyone?
这没有任何帮助。有什么想法吗?
4 个解决方案
#1
2
I had this exact problem, and I was only able to get it to work by using ".serializeArray()", I hope this was what you were looking for.
我遇到了这个问题,我只能通过使用“.serializeArray()”来使它工作我希望这就是你想要的。
data: {
'options':$('input[name="options[]"]').serializeArray()
},
For me this outputs standard string in the same format as GET requests.
对于我来说,它以与GET请求相同的格式输出标准字符串。
更多相关文章
- php怎么读取MYSQL数据到radio选项
- 如何在HTML选择选项列表中保留空间hi
- 如何让react-native Picker保持新选择的选项?
- 如何为select中的选项创建?
- AngularJS(1.5.8) - 如何直接从获取json对象的控制器中填充选择选
- FieldErro:无法将关键字'date_added'解析为字段。选项包括:data_ad
- ubuntu 16.04 设置选项里面找不到《打印机》和《软件和更新》两
- Linux 3.4.39内核编译配置选项介绍
- ubi文件系统制作,还是"-c"选项的问题