good afternoon
I have the following code that what it tries to do is take the provinces and cantons, but I honestly do not know how to do so that when selecting the province, load the cantons that match the province ID
<html>
<head>
<title>Obteniendo las provincias</title>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function () {
$.ajax({
dataType: "json",
url: "https://ubicaciones.paginasweb.cr/provincias.json",
data: {},
success: function (data) {
var html = "<select>";
for(key in data) {
html += "<option value='"+key+"'>"+data[key]+"</option>";
}
html += "</select";
$('#destino').html(html);
}
});
})
</script>
<script>
$(document).ready(function () {
var n= '1';
$.ajax({
dataType: "json",
url:
"https://ubicaciones.paginasweb.cr/provincia/"+n+"/cantones.json",
data: {},
success: function (data) {
var html = "<select>";
for(key in data) {
html += "<option value='"+key+"'>"+data[key]+"</option>";
}
html += "</select";
$('#canton').html(html);
}
});
})
</script>
</head>
<div id="destino" ></div>
<div id="canton"></div>
</body>
</html>
As you can see in the second function I'm using a variable "var n = 1" in that way, because I'm sending the id of the province, what I need is for that variable to default to the value of the "id = destination" "
I hope you can help me.
Thank you very much for the information and suggestions since I am learning javascript and web services I am a beginner, now what I try to do with the code you gave me is to show me the districts. so I'm doing this:
$(document).on('change', '#canton > select', function() {
var provincia = this.value;
var canton = this.value;
$.ajax({
dataType: "json",
url: "https://ubicaciones.paginasweb.cr/provincia/" + provincia + "/canton/" + canton + "/distritos.json",
data: {},
success: function(data) {
agregarDistritos(data);
}
});
});
but when I select for example the 8th district of some canton I get an error because it takes the value of the canton also in the province:
jquery.min.js: 4 GET link 404 ()