我目前正在尝试返回数据库调用来填充下拉框。然而,当我循环访问返回的列表时,我收到“回调未定义”错误。我已经用两种方法尝试过这段代码,但都不起作用。
我试过了:
$('#Vehicle_KovId_value').change(function () {
var kovID = $(this).val();
var drop2 = $('#Vehicle_BodyStyle_value');
if (kovID != null && kovID != '') {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option('Please Select One', '-1');
$.ajax({
type: "GET",
url: '/Ajax/Index',
async: false,
data: { KovID: kovID },
contentType: "application/object; charset=utf-8",
success: function (record) {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option("Please Select One", "-1");
$.each(function (index, item) {
drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
});
},
error: function () {
$('#Vehicle_BodyStyle_value').get(0).options.length = 0;
$('#Vehicle_BodyStyle_value').get(0).options[0] = new Option("Error!", "-1");
alert("Failed to load styles");
}
});
}
});
我也尝试过:
$('#Vehicle_KovId_value').change(function () {
var kovID = $(this).val();
var drop2 = $('#Vehicle_BodyStyle_value');
if (kovID != null && kovID != '') {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option('Please Select One', '-1');
$.ajax({
type: "GET",
url: '/Ajax/Index',
async: false,
data: { KovID: kovID },
contentType: "application/object; charset=utf-8",
success: function (record) {
drop2.get(0).options.length = 0;
drop2.get(0).options[0] = new Option("Please Select One", "-1");
fillBStyles(record);
// $.each(function (index, item) {
// drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
// });
},
error: function () {
$('#Vehicle_BodyStyle_value').get(0).options.length = 0;
$('#Vehicle_BodyStyle_value').get(0).options[0] = new Option("Error!", "-1");
alert("Failed to load styles");
}
});
}
});
function fillBStyles(r) {
var drop2 = $('#Vehicle_BodyStyle_value');
$.each(function (index, item) {
drop2.get(0).options[drop2.get(0).options.length] = new Option(item.Display, item.Value);
});
}
这两个都给了我错误:
类型错误:回调未定义
返回的数据对象,record
,是我必须从中提取两部分的数据库对象列表。
如何修复此“回调”错误,以便我可以在函数中使用我的数据?