我正在mvc平台上进行jquery自动完成工作。现在,我的问题是,在某些文本框数据中,自动完成功能正常,而在某些文本框数据中,自动完成功能无法正常工作。
为了更清楚,让我们看看自动完成任务的图像
现在按照我写R字的图像,我得到相关R字的建议列表。
现在按照第二张图片,我写了整个单词,但仍然没有显示建议。
这是我的代码,
视图
<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 });
}
随着你的到来
未捕获的TypeError:无法读取未定义的属性'substr'
对于此错误,您应检查data
是否为空。
success: function (data) {
if(data != null)
{
autocomplete(document.getElementById("CustomerName"), data.data);
}
}