typeahead自动完成投掷错误

时间:2016-07-13 12:31:27

标签: javascript jquery asp.net-mvc typeahead.js

您好我正在尝试使用" typeahead.js"来实现自动完成文本框。像我的MVC项目中的http://sriniavatar.com/bootstrap-typeahead-in-asp-net-mvc/

我的剧本吼叫

<script type="text/javascript">
    var substringMatcher = function (strs) {
        return function findMatches(q, cb) {
            var matches, substringRegex;               
            matches = [];                
            substrRegex = new RegExp(q, 'i');                
            $.each(strs, function (i, str) {
                if (substrRegex.employees(str)) {
                    matches.push(str);
                }
            });

            cb(matches);
        };
    };

    $.post('/Employee/TagSearch', null, function (data) {
        var employees = data;
        $('#sriniavatar').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
        },
        {
            name: 'employees',
            displayKey: 'value',
            source: substringMatcher(employees)
        });
    });

我的员工控制器的TagSearch方法如下所示

[HttpPost]
    public ActionResult TagSearch(string term)
    {
        DataAccess db = new DataAccess();
        var employees = db.GetEmployeesName().ToList();
        //if (!String.IsNullOrEmpty(term))
        //{
        //    employees = employees.Where(e => e.Surname.Contains(term.Trim())
        //    || e.ForeName.Contains(term.Trim())
        //    || e.Win_UserName.Contains(term.Trim())
        //    ).ToList();
        //}
        return Json(employees, JsonRequestBehavior.AllowGet);
    }

现在**问题是,当我运行程序时,它显示以下错误*&#34;第95行的未处理异常,http://localhost:57512/Employee/ViewEmployees中的第21列

0x800a01b6 - JavaScript运行时错误:对象不支持属性或方法&#39;员工&#39; ***
即使我参考了我的项目中的脚本

    <link href="~/Content/typeahead.css" rel="stylesheet" />        <script "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script> <script src="~/Scripts/typeahead.js"></script> <br/>...Page Contents...<br>
    <script src="~/Scripts/typeahead.mvc.model.js"></script>
    @RenderSection("scripts", required: false)

1 个答案:

答案 0 :(得分:1)

我不知道任何有regExp员工方法的JavaScript

if (substrRegex.employees(str)) {    
                ^^^^^^^^^

我相信你想要test

if (substrRegex.test(str)) {