我使用typeahead.js将产品id分配给一个隐藏的html表单域。当有匹配项时,这很有效:
$('.products_tt .typeahead').typeahead
name: 'products_tt',
prefetch:
url: '/products.json',
ttl: 60 * 5e3
template: '<p><strong>{{value}}</strong> – {{year}}</p>',
engine: Hogan
$('.products_tt .typeahead').on "typeahead:selected typeahead:autocompleted", (e,datum) ->
$('#disk_file_product_id').val(datum.id)当输入字段保留为空时,我将清除隐藏字段:
$('.products_tt .typeahead').blur ->
if $(this).val().length == 0
$('#disk_file_product_id').val("")但是,当在输入字段中输入了文本但没有匹配时,我还需要清除隐藏字段。
我的Java/Coffeescript技能很弱,所以不知道怎么做?!?
发布于 2013-08-04 14:47:11
以下其中一项应该可以工作:
1)首先连接到输入字段的change事件,然后清除那里的#disk_file_product_id。如果有选择,您的typeahead事件稍后将再次设置此选项。这也节省了您的blur回调。
$('.products_tt .typeahead').change ->
$('#disk_file_product_id').val("")2)在opened和closed事件中清除#disk_file_product_id,而不是在字段的change事件中清除。我不太喜欢这样。
$('.products_tt .typeahead').on "typeahead:opened typeahead:closed" ->
$('#disk_file_product_id').val("")发布于 2014-01-07 10:20:22
我知道这已经有几个月的历史了,但是你可以这样做:
使用jQuery查看每个keyup事件的DOM中有多少".tt-suggestions“。如果没有,请清除隐藏字段。
$('.products_tt .typeahead').typeahead({
name: 'products_tt',
prefetch: url: '/products.json',
ttl: 60 * 5e3
template: '<p><strong>{{value}}</strong> – {{year}}</p>',
engine: Hogan
}).on("typeahead:selected typeahead:autocompleted", function (e, datum) {
$('#disk_file_product_id').val(datum.id);
}).on('keyup', function () {
if($('.tt-suggestion').length === 0){
$('#disk_file_product_id').val("");
}
});https://stackoverflow.com/questions/18036078
复制相似问题