您好我需要使用jquery bootstrap typeahead触发show,并尝试模拟按键但无法显示typeahead。
预先确定是否有触发与按键相同效果的事件?
有什么建议吗?
答案 0 :(得分:3)
另请阅读Definitive way to trigger keypress events with jQuery。问题似乎是你首先必须关注,所以尝试使用这个函数:
function triggertypeahead(element,value,empty)
{
empty = typeof empty !== 'undefined' ? false : true;
element.focus();
element.val(value);
element.trigger(jQuery.Event('keyup'));
if(empty)element.val('');
}
示例强>:
<input id="typeaheadinput" type="text" class="span3" style="margin: 0 auto;" data-provide="typeahead" data-items="4" data-source='["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]'>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="bootstrap-master/js/bootstrap-typeahead.js"></script>
<script>
function triggertypeahead(element,value,empty)
{
empty = typeof empty !== 'undefined' ? false : true;
element.focus();
element.val(value);
element.trigger(jQuery.Event('keyup'));
if(empty)element.val('');
}
triggertypeahead($('#typeaheadinput'),'al');
</script>