我正在使用这个jquery用选项填充select元素:
$("#ticket_customer").change(function() {
$.ajax({
url: "?getContactList=2&customer=" + $(this).val(),
async: true,
success: function(data) {
var sel = $("#ticket_contact");
sel.empty();
for (var i=0; i<data.length; i++) {
sel.append('<option value="' + data[i].sequence + '">' + data[i].forename + '</option>');
}
//$("#ticket_contact").html(data);
GetCustomerDetails();
}
});
});
对URL的请求是按以下格式返回数据:
[{"sequence":"465","forename":"first1","surname":"second1","email":"email@domain.com"},{"sequence":"465","forename":"first2","surname":"second2","email":"email2@domain.com"}]
但它没有填充元素
答案 0 :(得分:1)
您必须添加dataType以指定您使用的格式:
$("#ticket_customer").change(function() {
$.ajax({
url: "?getContactList=2&customer=" + $(this).val(),
async: true,
dataType : "json",
success: function(data) {
var sel = $("#ticket_contact");
sel.empty();
for (var i=0; i<data.length; i++) {
sel.append('<option value="' + data[i].sequence + '">' + data[i].forename + '</option>');
}
//$("#ticket_contact").html(data);
GetCustomerDetails();
}
});
});