简体   繁体   English

如何将Tab键转换为Web的Enter键?

[英]How to Convert A Tab Key Press Into An Enter Key Press For Web Pages?

I am create a form in html for submit data. 我正在用html创建一个表单来提交数据。 When I shall reach save button by tab press and press tab again then tab will act as a Enter key. 当我按Tab键到达保存按钮并再次按Tab键时,Tab键将充当Enter键。 That means data will save. 这意味着数据将保存。 How can i do this? 我怎样才能做到这一点?

I have use the following code 我已经使用以下代码

 <form id="form">
<input type="text" />
<input type="text" />
<input type="text" />
<input disabled="disabled" />
<input readonly="readonly" value="readonly" />
<textarea></textarea>

<input type="submit" />

$(":input").on("keydown", function(event) {
    if (event.which == 9) {
        event.startPropagation();
    }
});

Please tell me the solution. 请告诉我解决方案。

an alternative to mjalajel 's answer mjalajel的答案的替代品

$("input[type='submit']").on("keyup", function(event) {
    if (event.which == 9) {
        $(this).trigger('click');
    }
});

this way whatever you have on click event is called 这样,无论您点击事件如何,都称为

if you want to follow a link, you have to replace 如果您想点击链接,则必须替换

$(this).trigger('click');

with

this.click();

this is because jquery click event isn't the same as a "real" click more info here: jQuery: how to trigger anchor link's click event 这是因为jquery click事件与“真正的” click事件不同,请单击此处: jQuery:如何触发锚链接的click事件

I assume this is what you're trying to achieve 我认为这就是您要实现的目标

$("input[type='submit']").on("keyup", function(event) {
    if (event.which == 9) {
        $('#form').submit();
    }
});

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

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