需要在隐藏字段中获取所选值的ID,但始终返回undefined。控制器的动作和脚本如下。Jquery UI - 自动完成自定义错误
我在做什么错在这里。我完全是这个UI自动完成的新手。
所以需要帮助来解决这个问题。
('#PanelSearchKeyword').autocomplete({
source: function(request, response) {
var params = {};
params.supplieTyperName = document.getElementById('PanelSearchKeyword').value;
$.ajax({
type: 'POST',
dataType: "json",
url: '/Home/GetSupplierTypeNameList',
data: AddAntiForgeryToken(params),
success: function(data) {
var array = data.map(function(element) {
return {
value: element['SupplierTypeName'],
id: element['SupplierTypeId']
};
});
response(array);
},
error: function(xhr, ajaxOptions, thrownError) {
logError(ajaxOptions, thrownError);
}
});
},
select: function(event, ui) {
$("#SuppTypeId").val(ui.item.SupplierTypeId); // save selected id to hidden input
var a = $("#SuppTypeId").val();
alert(a);
//return false;
},
minLength: 2
});
我的控制器动作看起来像
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult GetSupplierTypeNameList(string supplieTyperName)
{
try
{
List<SupplierTypeViewModel> supplierTypeNameList = new List<SupplierTypeViewModel>();
supplierTypeNameList = (from supplierType in db.PPSupplierTypes
where supplierType.PPSupplierTypeName.ToUpper().StartsWith(supplierTypeName.ToUpper())
orderby supplierType.PPSupplierTypeName
select new SupplierTypeViewModel
{
SupplierTypeId = supplierType.PPSupplierTypeId,
SupplierTypeName = supplierType.PPSupplierTypeName
}).ToList();
return supplierTypeNameList;
}
catch (Exception)
{
throw;
}
return Json(suppplierTypeResult, JsonRequestBehavior.AllowGet);
}
尝试'ui.item.value'或'ui.item.id' – Tushar
@Tushar我已经尝试过这一点,但结果是相同的“未定义”。 –
你可以创建一个'jsfiddle'演示这个 – Tushar