[英]Navigating through text input fields using arrow keys and return
我正在尝试使用jQuery在多个输入字段之间构建一种简单的导航机制。 代码的第一部分,使用向下箭头或返回键向下滑动可以正常工作,但是当我添加第二个块以通过查找向上箭头然后反转顺序来向后移动时,在第一个文本字段中键入会向右跳走到第二。 有什么想法吗?
<script type="text/javascript">
$(document).ready(function(){
// get only input tags with class data-entry
textboxes = $("input.data-entry");
// now we check to see which browser is being used
if ($.browser.mozilla) {
$(textboxes).keypress (checkForAction);
} else {
$(textboxes).keydown (checkForAction);
}
});
function checkForAction (event) {
if (event.keyCode == 13 || 40) {
currentBoxNumber = textboxes.index(this);
if (textboxes[currentBoxNumber + 1] != null) {
nextBox = textboxes[currentBoxNumber + 1]
nextBox.focus();
nextBox.select();
event.preventDefault();
return false;
}
}
if (event.keyCode == 38) {
currentBoxNumber = textboxes.index(this);
if (textboxes[currentBoxNumber - 1] != null) {
prevBox = textboxes[currentBoxNumber - 1]
prevBox.focus();
prevBox.select();
event.preventDefault();
return false;
}
}
}
</script>
将if (event.keyCode == 13 || 40) {...
更改为if (event.keyCode == 13 || event.keyCode == 40) { ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.