I am trying to create a cascading dropdown with all the 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 for loading items using ajax, both work 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 site selection. I have it:
$(document).ready(function () { $("#Sites").on("change", function (e) { var siteId = $("#Sites").val(); GetSectors(siteId); }); GetSites(); });
But that will not work. I am using jquery 1.8.3.
Any idea where the problem is?
Thanks!
estebane97
source share