如何对从 AJAX 请求中获取的响应进行排序并将它们映射为 <option> 到 <select> 标记?
how do I sort response fetched from AJAX request and map them as <option> to <select> tag?
我正在尝试使用 AJAX 对从后端获取的响应进行排序。这是我的代码:
let $select = $("#colony_code");
$.ajax({
url: 'backend.php',
type: 'POST',
data: { "input": "fetch_colony_codes" },
dataType: 'json',
success: function(response) {
let selectedValue = $select.val();
let html = response.filter((e, i, a) => a.indexOf(e) === i).map(item => `<option value="${item}">${item}</option>`);
$select.html(html).val(selectedValue);
},
complete: function() {}
});
现在,我希望在我的 <select>
标签中对所有 options
进行排序。如何对选项进行排序?
伙计,我很愚蠢。只需在 success(..)
的开头添加 response.sort();
即可解决我的问题。 XD
我正在尝试使用 AJAX 对从后端获取的响应进行排序。这是我的代码:
let $select = $("#colony_code");
$.ajax({
url: 'backend.php',
type: 'POST',
data: { "input": "fetch_colony_codes" },
dataType: 'json',
success: function(response) {
let selectedValue = $select.val();
let html = response.filter((e, i, a) => a.indexOf(e) === i).map(item => `<option value="${item}">${item}</option>`);
$select.html(html).val(selectedValue);
},
complete: function() {}
});
现在,我希望在我的 <select>
标签中对所有 options
进行排序。如何对选项进行排序?
伙计,我很愚蠢。只需在 success(..)
的开头添加 response.sort();
即可解决我的问题。 XD