我使用了typeahead的“notFound”选项中的“template”,从那里我可以设置一个链接按钮,对我来说更加方便。Typeahead设置:
$('#remote .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
name: 'sn-tags',
display: 'tag',
source: oBusqTags,
templates: {
notFound: '<p>No matches<br><a id="btnAddNewTag" href="#">Add New Tag</a></p>'
}
}).bind("typeahead:select", function(obj, datum, name) {
$('#lbxAddTag').append($('<option>', {
value: datum.id,
text: datum.tag
}));
});
这是该链接的监听器:
$('#remote').on('click', '#btnAddNewTag', function(e){
$('#lbxAddTag').append($('<option>', {
value: "0",
text: $('#tbxBusqTag').val()
}));
$('#remote .typeahead').typeahead('val','');
e.preventDefault();
});