嗨,我需要使用 jquery bootstrap typeahead 触发显示,并尝试模拟按键但无法显示 typeahead。
Typeahead 是否有任何事件触发与按键相同的效果?
有什么建议么?
问问题
457 次
1 回答
3
另请阅读使用 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>
于 2013-06-28T22:09:33.203 回答