【发布时间】:2015-07-21 09:40:08
【问题描述】:
我通过 ajax 将多维数组从 php 脚本返回到 html 页面。现在数组中的值已正确返回。我在 console.log 上检查了它。
但是如何在 jquery 中将每个数组更改为单独的数组?
这就是我传递表单 php 脚本的方式
$json=array();
array_push($json,array("type"=>$carType,"maker"=>$carMaker,"rate"=>$selectRate));
echo json_encode($json);
这就是它在 ajax 中的处理方式。
$("#submit").on("click",function()
{
$("#set_setting").submit(function(){
data = $(this).serialize()
$.ajax({
type: "POST",
dataType: "html",
url: "submit_setting.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//hide the form
$("#set_setting").slideUp("slow");
//show the result
console.log(data);
$(".the-return").html(data);//how to show in table here?
}
});
return false;
});
});
console.log(data) 输出如下:
[
{
"type":["4 wheeler","flying ycar"],
"maker":["Honda","Audi"],
"rate":[["2","20"],["2","40"],["2","50"],["0","80"],["0","90"],["0","70"]
}
]
我想将类型、制造商和评分显示为单个表格。 喜欢,
类型:四轮车,飞行汽车[在选择框中]
制造商:本田、奥迪[在选择框中]
费率:值 2- 20,40,50 [表中]
值 0- 80,90,70 [在表中]
我知道这有点复杂。至少如果可以在不同的表中显示每个数组就足够了,但在 Jquery 中。
编辑后
$("#submit").on("click",function()
{
$("#set_setting").submit(function(){
data = $(this).serialize()
$.ajax({
type: "POST",
dataType: "json",
url: "submit_setting.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
//hide the form
$("#set_setting").slideUp("slow");
//show the result
var parse_JSON = function (data) {
try {
var obj = JSON && JSON.parse(data) || $.parseJSON(data);
return obj;
} catch (e) {
// not json
console.log("Can not parse");
return false;
}
};
$.each( obj.type, function( index, value ){
console.log(value);
});
//$(".the-return").html(me);
}
});
return false;
});
});
【问题讨论】: