-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
54 lines (50 loc) · 1.5 KB
/
index.js
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
// 1. what is API
// 2. How do I call API
// 3. Explain code
const host = "https://provinces.open-api.vn/api/";
var callAPI = (api) => {
return axios.get(api)
.then((response) => {
renderData(response.data, "province");
});
}
callAPI('https://provinces.open-api.vn/api/?depth=1');
var callApiDistrict = (api) => {
return axios.get(api)
.then((response) => {
renderData(response.data.districts, "district");
});
}
var callApiWard = (api) => {
return axios.get(api)
.then((response) => {
renderData(response.data.wards, "ward");
});
}
var renderData = (array, select) => {
let row = ' <option disable value="">chọn</option>';
array.forEach(element => {
row += `<option value="${element.code}">${element.name}</option>`
});
document.querySelector("#" + select).innerHTML = row
}
$("#province").change(() => {
callApiDistrict(host + "p/" + $("#province").val() + "?depth=2");
printResult();
});
$("#district").change(() => {
callApiWard(host + "d/" + $("#district").val() + "?depth=2");
printResult();
});
$("#ward").change(() => {
printResult();
})
var printResult = () => {
if ($("#district").val() != "" && $("#province").val() != "" &&
$("#ward").val() != "") {
let result = $("#province option:selected").text() +
" | " + $("#district option:selected").text() + " | " +
$("#ward option:selected").text();
$("#result").text(result)
}
}