[英]JQuery execute function when enter is pressed in a textbox
$(document).keypress(function(e) {
if(e.which == 13) {
$("#submitButton").click();
}
});
Thats what I have to execute my function when enter is pressed anywhere in a page, but that conflicts with my dropdown list
keyboard navigation . 那就是我在页面的任何地方按下Enter键时必须执行的功能,但这与我的
dropdown list
键盘导航冲突。
<div class="form-group" id="contractGroup">
<label for="inputContract" class="col-sm-5 control-label">Contract Type <b>*</b></label>
<div class="col-sm-7">
<div class="input-group" id="contractPicker">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><span class="caret"></span></button>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Full-time</a></li>
<li><a href="#">Part-time</a></li>
</ul>
</div>
<asp:TextBox runat="server" type="text" class="form-control" ID="inputContract" placeholder="Full time, Part time"></asp:TextBox>
</div>
</div>
</div>
When trying to tab through dropdown list
, you have to use enter to select item, which executes my function at same time as well, which I dont want. 当尝试通过
dropdown list
选项卡时,您必须使用enter选择项目,该项目也同时执行我的功能,这是我不希望的。
I know its possible to do function for each textbox
checking if enter was pressed while that textbox
have focus, but I have over 20+ and would rather avoid that. 我知道每个
textbox
可以执行功能,以检查该textbox
是否具有焦点时是否按下了Enter键,但是我有20多个,并且宁愿避免这种情况。 And I know that I can use asp:panel defaultbutton
but that ignores my jquery function and calls codebehind and not jquery function as I have 2 linking buttons
act as one and one of them cannot be asp:button
which is needed for asp:panel
. 而且我知道我可以使用
asp:panel defaultbutton
但是它会忽略我的jquery函数并调用codebehind而不是jquery函数,因为我有2个linking buttons
充当一个,并且其中一个不能是asp:panel
所需的asp:button
。
Another solution is to somehow to make dropdown list
treated differently and not as "current page". 另一个解决方案是以某种方式使
dropdown list
得到不同的对待,而不是将其视为“当前页面”。 I am using bootstrap datepicker
and while keyboard navigating through calendar popup, I can use enter without calling my function. 我正在使用引导
datepicker
并且在键盘上浏览日历弹出窗口时,可以使用enter而不调用函数。 If its possible I would love to have single function that detects if enter was pressed on any textbox
on a page. 如果可能的话,我希望拥有一个功能来检测是否在页面上的任何
textbox
按下了enter键。 Cheers 干杯
Try adding an id to the dropdown div and have a click listener tied to it to prevent it from bubbling up. 尝试在下拉div中添加一个id,并为其绑定一个点击侦听器,以防止其冒泡。 Something like...
就像是...
$("#dropdown-list").keypress(function(e) {
e.preventDefault();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.