jquery自动完成功能无法正常工作

问题描述 投票:0回答:1

我正在mvc平台上进行jquery自动完成工作。现在,我的问题是,在某些文本框数据中,自动完成功能正常,而在某些文本框数据中,自动完成功能无法正常工作。

为了更清楚,让我们看看自动完成任务的图像

enter image description here

现在按照我写R字的图像,我得到相关R字的建议列表。

enter image description here

现在按照第二张图片,我写了整个单词,但仍然没有显示建议。

这是我的代码,

视图

<input type="text" id="CustomerName" name="CustomerName" required data-provide="typeahead" class="typeahead search-query form-control autocomplete" 

placeholder="Customer Name" />

<script>
    $(document).ready(function () {
        $.ajax({
            url: "/ServiceJob/CustomerSerchAutoComplete",
            method: "GET",
            dataType: "json",
            minLength: 2,
            multiple: true,
            success: function (data) {                    
                /*initiate the autocomplete function on the "myInput" element, and pass along the countries array as possible autocomplete values:*/
                    //autocomplete(document.getElementById("CustomerName"), data.data);
                $('#CustomerName').autocomplete({ source: data.data, minLength: 2, multiple: true }); 
            }
        });
    });
</script>

调节器

[HttpGet]
public IActionResult CustomerSerchAutoComplete()
{
    var customers = _Db.Ledger.Where(x => x.LedgerTypeId == (int)LedgerType.Customer).ToList();
    var result = (from n in customers
                  select new
                  {
                      kk = n.Name.ToUpper()
                  }).ToList();

    return Json(new { data = result });
}
ajax asp.net-mvc jquery-ui-autocomplete
1个回答
0
投票

随着你的到来

未捕获的TypeError:无法读取未定义的属性'substr'

对于此错误,您应检查data是否为空。

 success: function (data) {                
                 if(data != null)
                   {
                    autocomplete(document.getElementById("CustomerName"), data.data);
                   }
                }
© www.soinside.com 2019 - 2024. All rights reserved.