【发布时间】:2018-12-20 09:18:06
【问题描述】:
我有一个包含 2 个输入字段的表单,我希望为这两个字段提供自动完成功能,但来源不同。虽然两者的代码基本相同,但它仅适用于一个领域(客户)。如果我使用 postman 手动查询 API,我会按预期获得数据。
这是表格
@model dynamic
@{
ViewBag.Title = "New Rental Form";
}
<h2>New Rental Form</h2>
<form>
<div class="form-group">
<label>Customer</label>
<input id="customer" type="text" value="" class="form-control" />
</div>
<div class="form-group">
<label>Movie</label>
<input id="movie" type="text" value="" class="form-control" />
</div>
<button class="btn btn-primary">Submit</button>
</form>
@section scripts
{
<script>
$(document).ready(function () {
var vm = {};
var customers = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/customers?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#customer').typeahead({
minLength: 3,
highlight: true
},
{
name: 'customers',
display: 'name',
source: customers
}).on("typeahead:select",
function (e, customer) {
vm.customerId = customer.id;
});
var movies = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/movies?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#movie').typeahead({
minLength: 3,
highlight: true
},
{
name: 'movie',
display: 'name',
source: movies
});
})
</script>
}
它仅适用于“客户”字段。同样在 Visual Studio 中,我可以看到查询已完成。那么也许问题在于解析 API 结果?我不知道...
【问题讨论】:
-
查看您发布的代码,您没有
.on("typeahead:select", function() {...})调用电影部分。 -
是的......这将是无用的,因为选择列表甚至没有弹出
标签: asp.net-mvc-5 twitter-typeahead