twitter-bootstrap 如果字段被聚焦,则在返回按键时引导输入字段调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30073014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap input field call function on return keypress if field is focused
提问by user3779703
I have an input group in bootstrap which is an icon, a field and a button - for the purposes of user interface experience I would like the return key to call the function attached to the search button but only if that field is in focus.
我在引导程序中有一个输入组,它是一个图标、一个字段和一个按钮 - 出于用户界面体验的目的,我希望返回键调用附加到搜索按钮的函数,但前提是该字段处于焦点状态。
Is this possible with native bootstrap or will some form of jQuery be needed? I wouldn't want the return keypress to be permanently used to activate this function, only if the user is using this input search field.
这是否可以使用本机引导程序或需要某种形式的 jQuery?我不希望返回按键永久用于激活此功能,仅当用户使用此输入搜索字段时。
Thanks in advance
提前致谢
<div class="form-group">
<label class="control-label" for="another-location">Search for another location</label>
<div class="input-group">
<div class="input-group-addon" data-toggle="tooltip" data-placement="right" title="Location"><span class="fa fa-map-marker fa-fw"></span></div>
<input type="text" class="form-control gllpSearchField" id="another-location" placeholder="A location" name="another-location" />
<span class="input-group-btn">
<button class="btn btn-default" type="button">Search</button>
</span>
</div>
</div>
回答by Raymond
Yes you need Jquery :
是的,你需要 Jquery :
$('#another-location').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('You pressed a "enter" key in textbox, here submit your form');
}
});

