【发布时间】:2018-10-09 18:55:51
【问题描述】:
我正在尝试添加选项以下拉网络服务器提供的数据。我打电话,我得到的只是 [Object Object] 以非 HTML 格式添加到下拉列表中。
$("div").on("click","#addproffbtn",function (event) {
//$(this).parent().children() select selection
var thing = $(this);
$.ajax({
url : '/cmanager/getprofesor',
type : 'GET',
success : function (data) {
for (let index = 0; index < data.length; index++) {
thing.parent().children()[1].append($('<option/>', {
value: data[index].firstName,
text: data[index].firstName
}));
}
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown
);
}
});
event.stopPropagation();
})
我也试过正常的:
append('<option value="'+data[index].firstName+'">'+data[index].firstName+'</option>')
【问题讨论】:
-
你能显示
data变量在做console.log(data)吗? -
尝试将
thing.parent().children()[1]替换为thing.parent().find('select')。您正在使用[0]选择本机 dom 节点并应用本机append的方法(不是 jQuery)。 -
@mrlew
closest()是给祖先的。<select>不太可能是祖先 -
@charlietfl 你说得对,我想是的。已更正!