I'm trying to create a cascade dropdown with all dynamic elements.
My Html:
<select id="Sites" name="SelectedSiteId"><option value=""></option></select>
<select id="Sectors" name="SelectedSectorId"><option value=""></option></select>
I have 2 functions to load elements using ajax, both working fine:
function GetSites() {
$.ajax({
url: '/Sites/GetSites',
dataType: "json",
type: "POST",
error: function () {
alert("An error ocurred.");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#Sites").html(items);
}
});
}
function GetSectors(siteId) {
$.ajax({
url: '/Sites/GetSectors',
data: { siteId: siteId },
dataType: "json",
type: "POST",
error: function () {
alert("An error ocurred.");
},
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$("#Sectors").html(items);
}
});
}
I need to call GetSectors based on the Site selection. I have this:
$(document).ready(function () {
$("#Sites").on("change", function (e) {
var siteId = $("#Sites").val();
GetSectors(siteId);
});
GetSites();
});
But it doesn't work. I'm using jquery 1.8.3.
Any Idea where is the problem?
Thank you!