【发布时间】:2017-06-27 10:19:06
【问题描述】:
在我的 mvc 视图中,我有一个从字典中获取值的 DropDownList
<div class="InputPart">
@(Html.Telerik().DropDownListFor(model => model.PasportAddressRegionID)
.Name("ddlfRegions1")
.BindTo(new SelectList((IEnumerable<Dictionary>)ViewData["Regions"], "ID", "Title"))
.ClientEvents(e => e.OnChange("changeRegion1"))
.HtmlAttributes(new { style = "min-width:200px" }))
</div>
我有 JS 函数 changeRegion1() 来获取这些值。
<script type="text/javascript">
function changeRegion1() {
var rgnId = $('#ddlfRegions1').data('tDropDownList').value();
$.ajax({
url: '@(Url.Action("_GetDistrict", "Staffs"))',
data: { regionId: rgnId },
type: "POST",
success: function (result) {
var combobox = $('#ddlfDistricts1').data('tDropDownList');
combobox.dataBind(result);
combobox.enable();
}
});
} </script>
我把函数放在同一个视图中,在所有代码下面,但是运行时我得到 Uncaught ReferenceError: changeRegion1 is not defined。函数本身没问题,但在我看来,事件处理程序在这里看不到它。 所以我只是想知道这里有什么问题?我应该如何引用该函数?
感谢任何帮助
【问题讨论】:
-
您确定页面上存在代码吗?如果您在浏览器中查看页面源代码,您会在那里看到它吗?
-
@JJJ 不,实际上我在页面源上找不到此代码,但它显示在视图上。嗯..你有什么想法如何处理这个?
-
@RinaMi 你在你的脚本被渲染之前检查控制台是否有任何错误?
-
解决方案:一个聪明的人建议将我的功能移到我的视图的开头,它现在可以工作了:)
标签: javascript asp.net-mvc referenceerror dropdownlistfor