-
Notifications
You must be signed in to change notification settings - Fork 122
/
contoh.html
177 lines (143 loc) · 5.99 KB
/
contoh.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
</head>
<body>
<label> Provinsi </label>
<select class="select2-data-array browser-default" id="select2-provinsi"></select>
<br>
<label> Kabupaten </label>
<select class="select2-data-array browser-default" id="select2-kabupaten"></select>
<br>
<label> Kecamatan </label>
<select class="select2-data-array browser-default" id="select2-kecamatan"></select>
<br>
<label> Kelurahan </label>
<select class="select2-data-array browser-default" id="select2-kelurahan"></select>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
var urlProvinsi = "https://ibnux.github.io/data-indonesia/provinsi.json";
var urlKabupaten = "https://ibnux.github.io/data-indonesia/kabupaten/";
var urlKecamatan = "https://ibnux.github.io/data-indonesia/kecamatan/";
var urlKelurahan = "https://ibnux.github.io/data-indonesia/kelurahan/";
function clearOptions(id) {
console.log("on clearOptions :" + id);
//$('#' + id).val(null);
$('#' + id).empty().trigger('change');
}
console.log('Load Provinsi...');
$.getJSON(urlProvinsi, function (res) {
res = $.map(res, function (obj) {
obj.text = obj.nama
return obj;
});
data = [{
id: "",
nama: "- Pilih Provinsi -",
text: "- Pilih Provinsi -"
}].concat(res);
//implemen data ke select provinsi
$("#select2-provinsi").select2({
dropdownAutoWidth: true,
width: '100%',
data: data
})
});
var selectProv = $('#select2-provinsi');
$(selectProv).change(function () {
var value = $(selectProv).val();
clearOptions('select2-kabupaten');
if (value) {
console.log("on change selectProv");
var text = $('#select2-provinsi :selected').text();
console.log("value = " + value + " / " + "text = " + text);
console.log('Load Kabupaten di '+text+'...')
$.getJSON(urlKabupaten + value + ".json", function(res) {
res = $.map(res, function (obj) {
obj.text = obj.nama
return obj;
});
data = [{
id: "",
nama: "- Pilih Kabupaten -",
text: "- Pilih Kabupaten -"
}].concat(res);
//implemen data ke select provinsi
$("#select2-kabupaten").select2({
dropdownAutoWidth: true,
width: '100%',
data: data
})
})
}
});
var selectKab = $('#select2-kabupaten');
$(selectKab).change(function () {
var value = $(selectKab).val();
clearOptions('select2-kecamatan');
if (value) {
console.log("on change selectKab");
var text = $('#select2-kabupaten :selected').text();
console.log("value = " + value + " / " + "text = " + text);
console.log('Load Kecamatan di '+text+'...')
$.getJSON(urlKecamatan + value + ".json", function(res) {
res = $.map(res, function (obj) {
obj.text = obj.nama
return obj;
});
data = [{
id: "",
nama: "- Pilih Kecamatan -",
text: "- Pilih Kecamatan -"
}].concat(res);
//implemen data ke select provinsi
$("#select2-kecamatan").select2({
dropdownAutoWidth: true,
width: '100%',
data: data
})
})
}
});
var selectKec = $('#select2-kecamatan');
$(selectKec).change(function () {
var value = $(selectKec).val();
clearOptions('select2-kelurahan');
if (value) {
console.log("on change selectKec");
var text = $('#select2-kecamatan :selected').text();
console.log("value = " + value + " / " + "text = " + text);
console.log('Load Kelurahan di '+text+'...')
$.getJSON(urlKelurahan + value + ".json", function(res) {
res = $.map(res, function (obj) {
obj.text = obj.nama
return obj;
});
data = [{
id: "",
nama: "- Pilih Kelurahan -",
text: "- Pilih Kelurahan -"
}].concat(res);
//implemen data ke select provinsi
$("#select2-kelurahan").select2({
dropdownAutoWidth: true,
width: '100%',
data: data
})
})
}
});
var selectKel = $('#select2-kelurahan');
$(selectKel).change(function () {
var value = $(selectKel).val();
if (value) {
console.log("on change selectKel");
var text = $('#select2-kelurahan :selected').text();
console.log("value = " + value + " / " + "text = " + text);
}
});
</script>
</html>