【发布时间】:2018-12-09 19:23:24
【问题描述】:
我正在处理的一个搜索表单中有输入,虽然一切正常,但我在找出问题时遇到了一些麻烦。它是搜索航班,搜索表单应该只发布特定的字母,但输入字段应该填写全名,所以我需要将特定的字母放在隐藏字段中。
这是我所拥有的一个例子。
$('.typeahead').typeahead({
source: function (query, process) {
states = [];
map = {};
var data = [{
DisplayName: '(Eindhoven (EIN))',
Code: 'EIN',
Type: 'Airport',
CityName: 'Eindhoven'
}];
$.each(data, function (i, state) {
map[state.DisplayName] = state;
states.push(state.DisplayName);
});
process(states);
},
matcher: function (item) {
if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
return true;
}
},
sorter: function (items) {
return items;//items.sort();
},
highlighter: function (item) {
var regex = new RegExp( '(' + this.query + ')', 'gi' );
return item.replace( regex, "<strong>$1</strong>" );
},
updater: function (item) {
SelectedValue = map[item].DisplayName;
SelectedCode=map[item].Code;
SelectedType=map[item].Type;
SelectedCityName=map[item].CityName;
return SelectedCityName+' ('+SelectedCode+')';
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<div class="col-md-4 col-sm-6">
<label for="let_od"><small class="text-uppercase text-muted"><?php _e('Od')?></small></label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text"><i class="icon-location-8"></i></div>
</div>
<input type="text" class="form-control typeahead" data-provide="typeahead" name="let_od" id="let_od" placeholder="(grad ili aerodrom)" autocomplete="off"/>
<input type="hidden" name="let_od_iso" id="let_od_iso" autocomplete="off" value=""/>
</div>
</div>
<div class="col-md-4 col-sm-6">
<label for="let_do"><small class="text-uppercase text-muted"><?php _e('Do' )?></small></label>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text"><i class="icon-location-8"></i></div>
</div>
<input type="text" class="form-control typeahead" data-provide="typeahead" name="let_do" id="let_do" placeholder="(grad ili aerodrom)" autocomplete="off"/>
<input type="hidden" name="let_do_iso" id="let_do_iso" autocomplete="off" value=""/>
</div>
</div>
因此,当我在某个字段中输入任何内容时,例如在"let_od" 输入字段中,预先输入搜索并返回该字段的正确值。示例:(Eindhoven (EIN)) 但我只需要提交航班代码,因此我需要仅使用 EIN 值填充隐藏字段。
填充所选输入时,预输入也可以填充隐藏字段吗?
【问题讨论】:
标签: javascript jquery typeahead.js