I have a form and user will select city. When the user selects the city, the town list will change to bring it to the cities of the towns.
I want to delete the towns of non-selected cities.
I have a code but it does not work :
<div class="form-group">
<label for="city">City :</label>
<select name="city" id="city" class="form-control" required onchange="townChange()">
@foreach (var cityItem in Model.cityList)
{
<option value="@cityItem.Id">@cityItem.Name</option>
}
</select>
</div>
<div class="form-group">
<label for="town">Town :</label>
<select name="town" id="town" class="form-control" required>
@foreach (var townItem in Model.townList)
{
<option value="@townItem.Id" accesskey="@townItem.CityId">@townItem.Name</option>
}
</select>
</div>
function townChange()
{
var cityId = document.getElementById("city").value;
var townlist = document.getElementById("town");
for (var i = 0; i < townlist.length; i++) {
if (townlist.options[i].accessKey == cityId)
townlist.remove(i);
}
}