我有一个表单,我在客户字段中使用typeahead,这显示了客户名称,但我需要将客户ID保存到数据库中,而不是名称。
以下是我正在尝试的内容:
app.blade.php
<div class="form-group">
{!! Form::label('client', 'Client:') !!}
{!! Form::text('client', '', ['class' => 'form-control', 'id'=>'clientsearch', 'autocomplete'=>'off']) !!}
</div>
<input id="clientID" name="clientID" hidden/>
形式
{{1}}
typeahead没有初始化,我得到的错误是:
“Uncaught TypeError:$(...)。typeahead不是函数”
答案 0 :(得分:0)
谢谢你们,我设法让它现在正常运行,以防万一其他人需要一些类似的东西代替代码
$('#client').typeahead({
source: function (query, process) {
client = [];
clientid = {};
var data = [{"clientID": "1", "clientName": "client 1"},
{"clientID": "2", "clientName": "client 2"},
{"clientID": "3", "clientName": "client 4"},
{"clientID": "4", "clientName": "client 10"},
{"clientID": "5", "clientName": "client 3"}
];
$.each(data, function (i, state) {
clientid[state.clientName] = state;
client.push(state.clientName);
});
process(client);
},
matcher: function (item) {
if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
return true;
}
},
sorter: function (items) {
return items.sort();
},
highlighter: function (item) {
var regex = new RegExp('(' + this.query + ')', 'gi');
return item.replace(regex, "<strong>$1</strong>");
},
updater: function (item) {
$('#clientID').val(clientid[item].clientID);
return item;
}
});