替换IE 11中的keyCode [英] Replace keyCode in IE 11
问题描述
我们使用此代码通过 Enter 键模拟 Tab 键:
We use this code for simulating Tab key with Enter key:
function EnterTab() {
if (event.keyCode == 13) event.keyCode = 9;
return false;
}
但是在IE 11中,keyCode
是只读,并且此代码不起作用.我该如何解决我的代码在IE 11中运行的问题?
But in IE 11 keyCode
is readonly and this code does not work. How I can solve this problem that my code run in IE 11?
推荐答案
我们使用此代码通过Enter键模拟Tab键:
...
当我按下Enter键时,我想将焦点转到下一个控件.
We use this code for simulating Tab key with Enter key:
...
When I hit Enter I want focus go to next control.
这个想法是错误的.请勿更改,进入选项卡.通过拦截Enter键来解决此问题,将焦点放在下一个元素上并取消该事件. jQuery解决方案:
The idea is wrong. Don't change enter to tab. Tackle this problem by intercepting the enter key, set the focus on next element and cancel the event. jQuery solution:
$(function() {
$("form").on("keypress", "input[type=text], select, textarea", function(e) {
if (e.which === 13) {
e.preventDefault();
var $fields = $(this).closest("form").find(":input");
var index = $fields.index(this);
$fields.eq(index + 1).trigger("focus");
}
});
});
input,
select,
textarea {
box-sizing: border-box;
margin: .5em 0;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form method="post">
<p>Hitting enter inside the text fields will not submit the form</p>
<textarea rows="4"></textarea>
<input type="text" />
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<input type="text" />
<input type="submit" value="sumbmit" />
</form>
用于在Internet Explorer中测试的jsFiddle
这篇关于替换IE 11中的keyCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!