【问题标题】:JS function is not defined. How to reference the function?JS 函数未定义。如何引用函数?
【发布时间】: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


【解决方案1】:

这里是 ASP.Net 和 MVC 的 Kendu UI 下拉列表事件处理示例。与您的代码进行比较。该示例显示事件,您调用 ClientEvents 否则看起来没问题。

http://docs.telerik.com/aspnet-mvc/helpers/dropdownlist/overview

        @(Html.Kendo().DropDownList()
      .Name("dropdownlist")
      .BindTo(new string[] { "Item1", "Item2", "Item3" })
      .Events(e => e
            .Select("dropdownlist_select")
            .Change("dropdownlist_change")
      )
    )
    <script>
    function dropdownlist_select() {
        //Handle the select event.
    }

    function dropdownlist_change() {
        //Handle the change event.
    }

【讨论】:

  • 谢谢你的回答,但是这个例子使用了有点不同的 Kendo().DropDownList()
  • @RinaMi 我的道歉。
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 2015-12-06
  • 2019-09-05
  • 2011-09-19
  • 2017-10-17
  • 1970-01-01
相关资源
最近更新 更多