javascript - cascade dropdown in mvc value change
问题描述
I have three relational dropdown like if i select the program dropdown then it should populate the years and when i select the year it should populate blocks , i wrote the javascript for it it is working fine but there is an issue like on page load the events are not fired i need to select the dropdown list value and then change it to fire the event secondly on third dropdown its value did not get refreshed when i changed the dropdown of first dropdown list can someone suggest me how to fix these two issues below is my code
$("#ddlProgram").change(function () {
$("#ddlYear").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("getCity")',
dataType: 'json',
data: { id: $("#ddlProgram").val() },
success: function (states) {
$.each(states, function (i, state) {
$("#ddlYear").append('<option value="' + state.Value + '">' +
state.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve Year information.' + ex);
}
});
return false;
})
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#ddlYear").change(function () {
$("#ddlBlock").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("getBlocks")',
dataType: 'json',
data: {
id: $("#ddlYear").val(),
programid: $("#ddlProgram").val()
},
success: function (states) {
$.each(states, function (i, state) {
$("#ddlBlock").append('<option value="' + state.Value + '">' +
state.Text + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve information.' + ex);
}
});
return false;
})
});
</script>```
解决方案
推荐阅读
- php - 如何在 PrestaShop 中提交付款之前执行操作?
- javascript - 倒计时动画循环 - CreateJS / EaselJS / TweenJS
- python - how to find the frequency of marks each mark from 0 to total marks in a tuple
- node.js - 来自猫鼬文档的字符串值添加到变量中的奇怪行为
- sharepoint - 检查用户是否已登录 SharePoint?
- c# - 使用具有相同 DbContext 的多个实例的 EF 的 MVC 应用程序的生产部署
- sql - 具有特定截止时间的 DATEDIFF
- javascript - 如何从 vue el-date-picker 中修剪/删除时间?
- python - 每 n 个字符拆分字符串,但不拆分单词
- java - Maven不加载对本地工件的依赖