2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
您必須為每個(gè)選項(xiàng)創(chuàng)建新選項(xiàng)。否則修改相同。
$(function() {
var province = $("#provinces");
var select = $("#selection");
$.ajax({
type: "GET",
dataType: "json",
url: "http://dev.farizdotid.com/api/daerahindonesia/provinsi",
success: function(provinces) {
console.log(provinces);
var allProvince = provinces.semuaprovinsi;
$.each(allProvince, function(i, item) {
var option = $("<option>");
option.text(item.nama);
option.attr("value", item.id);
select.append(option);
});
}
});
});
$(function() {
var select = $("#selection");
$.ajax({
type: "GET",
dataType: "json",
url: "http://dev.farizdotid.com/api/daerahindonesia/provinsi",
success: function(provinces) {
var allProvince = provinces.semuaprovinsi;
$.each(allProvince, function(i, item) {
var option = $("<option>");
option.text(item.nama);
option.attr("value", item.id);
select.append(option);
});
}
});
});
**//Working sample**
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js"></script>
</head>
<body>
<select id="selection">
<option value=""></option>
</select>
</body>
</html>

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
在每次迭代option中初始化:.each
$.each(allProvince, function (i, item) {
var option = $('<option>');
option.text(item.nama);
option.attr('value', item.id);
select.append(option);
});
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)