简体   繁体   English

在文本框中按下Enter时的jQuery执行功能

[英]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.

相关问题 在文本框中按下 Enter 时如何触发 javascript 函数 - How to trigger a javascript function when enter is pressed in a textbox 按下回车键时如何执行JavaScript函数 - How can I execute JavaScript function when the enter key is pressed 如果文本框具有焦点,则在按特定键时执行功能 - Execute function when particular key is pressed if textbox has focus 按Enter键时如何进入下一个文本框? - How to go to next textbox when enter is pressed? 当按下enter时运行javascript函数 - JavaScript只有Jquery - Run javascript function when enter is pressed - JavaScript only no Jquery 当焦点在2个文本框中的某个特定文本框上时,如果按下Enter键,则会出现警报消息 - Alert message if enter is pressed when focus in on a particular textbox out of 2 textbox jQuery-在键盘上按下后续按钮时,在textbox / textarea中输入文本 - jquery- Enter text into textbox/textarea as subsequent button is pressed on the keyboard 在输入(文本框)字段中按Enter键时验证并提交表单,但在按Enter键时不选择历史记录 - validate and submit form when enter is pressed in input(textbox) field but does not select history when enter pressed 如何使用jQuery检测何时按下回车键? - How to detect when an enter is pressed using jQuery? 在文本框中按下 Enter 键时,重定向到 page.aspx - When Enter Key is pressed in a textbox, redirect to page.aspx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM