谢谢,我现在通过 json 传递数组并将数据类型设置为 json,按照您的示例如下:
{"car_make_id":"3","car_engine_type":"非涡轮汽油"}{"car_make_id":"3","car_engine_type":"非涡轮柴油"}
但是我的脚本仍然拒绝附加选择选项和值?:
$("select#make").change(function(){
$('#engine').find('option').remove().end(); //clear the engine ddl
var make = $(this).find("option:selected").val(); //Need the value not the text - .Text()
//alert(make);
//do the ajax call
$.ajax({
url:'get-engine.php',
type:'GET',
data:{engine:make},
dataType:'json',
cache:false,
success:function(data){
//var parsedjson = data=JSON.parse(data); //no need if data:type json
$.each(data, function(index, item) {
$("<option/>", {
value: item.car_make_id,
text: item.car_engine_type
}).appendTo("#engine");
});
}
});
});
似乎无法找到为什么它不会解析数组?
最佳答案
JSON 结构很糟糕,所以真正的问题是修复 JSON 使其符合逻辑。无论如何 您可以在当前的情况下尝试以下操作:
$.ajax({
url: 'grabdata.php',
type: 'GET',
data: {
type: make
},
dataType: "json",
cache: false,
success: function (data) {
$.each(data, function (index, item) {
for( var key in item ) {}
$("<option/>", {
value: key,
text: item[key]
}).appendTo("#engine");
});
},
error: function (jxhr) {
alert(jxhr.responseText);
}
});
jsfiddle:http://jsfiddle.net/DBg2L/1/
关于php - 解析 JSON 数组并提取选择列表 ID 和值的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456616/