我是javascript和jquery的新手。我正在尝试创建一个以模型数据为源的自动完成jqueryui。我不知道怎样做才是最好的办法。
我试图初始化文档中的数据。准备如下:
var listAddress = [];
foreach (var item in Model.allBuildings)
{
//adding into address array all addresses for label and its id.
@: listAddress.push({ label: "@Html.Raw(item.Address)", id: "@item.ApartmentBlockID" });*@
}
自动完成功能可以工作,但我一直从开发人员工具获取消息
Violation] 'setTimeout' handler took 113ms
我的问题是,如何更好地使用模型数据作为自动完成的源?我最大的困惑是我没有在任何地方设置setTimeout函数!错误指向jqueryui脚本中的setTimeout函数??
更新:
这是我的观点
// first autocomplete
<div class="col-md-10">
@Html.HiddenFor(model => model.renovationDetail.ApartmentBlockID, new { @id = "hidden_apartblockID" })
@Html.EditorFor(model => model.BuildingID, new { htmlAttributes = new { @class = "form-control", @id = "show_buildingID" } })
@Html.ValidationMessageFor(model => model.renovationDetail.ApartmentBlockID, "", new { @class = "text-danger" })
</div>
</div>
//second autocomplete
<div class="form-group">
@Html.LabelFor(model => model.allBuildings.First().Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control", @id = "show_address" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div
This is my javascript for address autocomplete(I do the same for the other one):
////function to load building addresses when page loads.
function ChangeAddressForSelect() {
//creating autocomplete for address
$('#show_address')
.blur(
function () {
var keyEvent = $.Event("keydown");
keyEvent.keyCode = $.ui.keyCode.ENTER;
$(this).trigger(keyEvent);
// })
.autocomplete({
//source: '/Renovations/GetAddressForEdit',
source: function (request, response) {
response($.ui.autocomplete.filter(listAddress,
request.term));
},
minLength: 0,
scroll: true,
select: function (event, ui) {
//set tagids to save
//$("#hidden_apartblockID").val(ui.item.id);
//// Tags for display
//this.value = ui.item.value;
return false;
},
focus: function () { $(this).autocomplete("search"); return false; },
.blur(function () {
//$(this).autocomplete('enable');
});
使用模型数据作为每个autcomplete的源的最有效方法是什么?我应该切换到ajax还是ajax源数据会减缓页面加载?