简体   繁体   English

防止插入字符并引发新事件

[英]Prevent from inserting char, and firing new event

I have problem with, capturing "keydown" and then change focus to "last active" focus. 我在捕获“ keydown”然后将焦点更改为“上次活动”焦点时遇到了问题。

If I set focus to particular element within function that captures event, "input" element get a "char" afterwards , or whole "form" element capture "enter" to submit form again. 如果将焦点设置为捕获事件的函数中的特定元素,则“ input”元素之后将获得一个“ char”,或者整个“ form”元素将捕获“输入”以再次提交表单。

My code: 我的代码:

http://jsfiddle.net/BluesM/KkexX/4/ http://jsfiddle.net/BluesM/KkexX/4/

I found way but this is clunky. 我找到了路,但是这很笨拙。 Within function that capture event I do 在捕获事件的功能中

 setTimeout(function(){newFocus.focus();  }, 30);

where 哪里

newFocus

Is the element which I want set focus on. 是我要重点关注的元素。

But maybe there is a better way? 但是也许有更好的方法?

Simply preventDefault() on keydown after form is submited. 提交表单后,只需在keydown上使用preventDefault()

http://jsfiddle.net/KkexX/7/ http://jsfiddle.net/KkexX/7/

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

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