我有一个里面有一个多选的表格。提交时我有这个功能:
$( "#addTrainForm" ).submit(function( event ) {
var dataString = $(this).serializeArray();
console.log(dataString);
event.preventDefault();
$.ajax({
type: "POST",
url: "/index.php/trainings/insertTraining",
data: dataString,
dataType: 'json',
success: function(data) {
$("div#addModal").hide();
location.reload();
}
});
return false;
});
现在您可以看到该控制台日志,我得到以下信息:
0: Object
name: "date"
value: "14/10/2013 16:22:46"
__proto__: Object
1: Object
name: "pool"
value: "1"
__proto__: Object
2: Object
name: "repeat_0"
value: "1"
__proto__: Object
3: Object
name: "distance_0"
value: "1"
__proto__: Object
4: Object
name: "style_0"
value: "1"
__proto__: Object
5: Object
name: "change_0"
value: "1"
__proto__: Object
6: Object
name: "time_0"
value: "1"
__proto__: Object
7: Object
name: "options_0"
value: "1"
__proto__: Object
8: Object
name: "options_0"
value: "2"
__proto__: Object
9: Object
name: "repeat_1"
value: "2"
__proto__: Object
10: Object
name: "distance_1"
value: "2"
__proto__: Object
11: Object
name: "style_1"
value: "1"
__proto__: Object
12: Object
name: "change_1"
value: "2"
__proto__: Object
13: Object
name: "time_1"
value: "2"
__proto__: Object
14: Object
name: "options_1"
value: "4"
__proto__: Object
15: Object
name: "options_1"
value: "5"
__proto__: Object
16: Object
name: "options_1"
value: "6"
您可以在哪里看到 options_0 和 options_1 有多个值。
现在在调用 get 的 php 函数中,我读取所有发布数据并将其直接写入文件:
public function insertTraining(){
$data=$this->input->post();
$this->baselib->logIt(print_R($data,true));
}
在那里我的 options_0 和 options_1 只有最后一个可用的值。我期待更多的值数组或逗号分隔的字符串或其他东西。这是我在文本文件上的日志的样子:
Array
(
[date] => 14/10/2013 16:22:46
[pool] => 1
[repeat_0] => 1
[distance_0] => 1
[style_0] => 1
[change_0] => 1
[time_0] => 1
[options_0] => 2
[repeat_1] => 2
[distance_1] => 2
[style_1] => 1
[change_1] => 2
[time_1] => 2
[options_1] => 6
)
如何获取 options_0 和 options_1 的所有值到我的 php 脚本?有没有更好的方法来使用我已经存在的函数(比如可选参数?)或者我是否必须围绕所有值运行一个循环并找到选项值并将它们放入一个数组中,然后使用 json 发送到 PHP?
希望我设法解释自己,谢谢你,詹姆斯