2 回答
TA贡献1848条经验 获得超10个赞
您必须为每个选项创建新选项。否则修改相同。
$(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贡献1812条经验 获得超5个赞
在每次迭代option中初始化:.each
$.each(allProvince, function (i, item) {
var option = $('<option>');
option.text(item.nama);
option.attr('value', item.id);
select.append(option);
});
- 2 回答
- 0 关注
- 95 浏览
添加回答
举报