简体   繁体   English

在不使用鼠标的情况下检查和取消选中控件,因为当我们按Enter键时,按钮控件被触发

[英]check and uncheck the control without using the mouse,as when we hit enter key the button control gets triggered

Currently i have a html design wherein which there is a textbox and a button..after typing the id on pressing enter the page will get redirect,but at the same time there is also a checkbox control. 目前,我有一个html设计,其中有一个文本框和一个按钮..按Enter键入id后,页面将被重定向,但同时也有一个复选框控件。

My question is : 我的问题是:

Is there any option to check and uncheck the control without using the mouse,as when we hit enter key the button control gets triggered 是否有任何选项可以在不使用鼠标的情况下检查和取消选中控件,因为当我们按Enter键时,按钮控件将被触发

For more idea ,i have given a signin window of gmail which contains a textbox and checkbox 对于更多的想法,我给了gmail登录窗口,其中包含一个文本框和复选框

在此处输入图片说明

This will click the button regardless of where the "Enter" happens on the page: 无论页面上“ Enter”出现在何处,都将单击该按钮:

$(document).keypress(function(e){
    if (e.which == 13){
        $("#Your_Submit_Button_ID").click();
    }
});

hitting enter key will trigger the submit button of the form you are currenlty focused in to. 按下Enter键将触发您当前专注于的表单的提交按钮。 Pressing tab will transfer the focus from one field to another but the enter key will always trigger the submit button of the form. 按Tab键会将焦点从一个字段转移到另一个字段,但Enter键将始终触发表单的提交按钮。

But you can trap the keypress when hitting enter and perform the check/uncheck instead of the default trigger. 但是,您可以在按下Enter键时捕获按键并执行检查/取消检查,而不是默认触发器。

$("form").keypress(function (e) {
    if(e.keyCode === 13) {
        e.preventDefault();
        // code that check/uncheck your checkbox
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM