如何根据需要显示Twitter的Bootstrap 2.x类型?

时间:2013-06-28 10:43:01

标签: twitter-bootstrap typeahead

您好我需要使用jquery bootstrap typeahead触发show,并尝试模拟按键但无法显示typeahead。
预先确定是否有触发与按键相同效果的事件?
有什么建议吗?

1 个答案:

答案 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>     

另请参阅:http://bootply.com/65987