我有以下javascript代码,它可以解决以下问题:下拉列表仅显示[object object] 我该如何解决该错误。
<script type="text/javascript">
$(document).ready(function () {
$("#RepfocusModelDropdown").change(function () {
var Id = $(this).val();
if (Id != null) {
$.ajax({
type: "POST",
url: "./Create?handler=UserSelect",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: {
Id: Id
},
dataType: "json",
success: function (response) {
$("#Repfocususerdropdown").empty();
alert(response);
$.each(response, function (i, item) {
$("#Repfocususerdropdown").append("<option>" + { value: item.Name, text: item.Name } + "</option>");
});
},
error: function (response) {
alert(response);
}
});
}
})
})
答案 0 :(得分:0)
在成功函数中使用以下代码:
var user = $('#Repfocususerdropdown').empty(), opt = null;
$.each(response, function (i, item) {
opt = $('<option/>', {"text": item.Name, "value": i});
user.append(opt);
});