Shr*_*uti 14 javascript jquery jquery-mobile
你可以告诉我当按下回车键时如何将焦点转移到下一个字段?我使用dform插件(将JSON转换为表单).
我用Google搜索,但这不起作用.为什么我的重点不转移到下一个领域?
$(document).keypress(function(e) {
if(e.which == 13) {
// Do something here if the popup is open
alert("dd")
var index = $('.ui-dform-text').index(this) + 1;
$('.ui-dform-text').eq(index).focus();
}
});Run Code Online (Sandbox Code Playgroud)
*注意(来自评论):它还需要处理没有tabindex设置值的页面
Gon*_*ing 29
它失败,因为this是document在你的代码.
您想要使用当前焦点项目的索引(document.activeElement),或者如果您使用委派事件,则可以确保this当前项目.
这个最终版本是否有效tabindexes.它也包裹着:
它们都使用我添加的自定义jQuery选择器来调用:focusable所有可聚焦元素(包括链接):
// register jQuery extension
jQuery.extend(jQuery.expr[':'], {
focusable: function (el, index, selector) {
return $(el).is('a, button, :input, [tabindex]');
}
});
$(document).on('keypress', 'input,select', function (e) {
if (e.which == 13) {
e.preventDefault();
// Get all focusable elements on the page
var $canfocus = $(':focusable');
var index = $canfocus.index(this) + 1;
if (index >= $canfocus.length) index = 0;
$canfocus.eq(index).focus();
}
});
Run Code Online (Sandbox Code Playgroud)
如果您愿意,可以在事件处理程序中使用相同的自定义选择器.然后它甚至可以在锚链接上工作(如果你将事件更改为keydown而不是keypress):
$(document).on('keydown', ':focusable', function (e) {
Run Code Online (Sandbox Code Playgroud)
这也使用委托on,监听keydown事件document.然后它应用jQuery选择器,然后将该函数应用于导致该事件的任何匹配元素.这样效率更高,因为它仅在事件时应用选择器(而不是将多个事件处理程序应用于每个DOM匹配元素).
$(document).keypress(function(e) {
if(e.which == 13) {
// Do something here if the popup is open
//alert("dd")
var index = $('.ui-dform-text').index(document.activeElement) + 1;
$('.ui-dform-text').eq(index).focus();
}
});
Run Code Online (Sandbox Code Playgroud)
*注意:警报可能会干扰focus,因此请console.log在大多数浏览器的调试窗口中使用此类输出并进行查看(如Chrome的F12调试工具).
这个包装回到最后一个项目并且也适用于选择(默认行为被阻止,因此您只能使用空格打开或向上/向下选择选项.
$('input,select').on('keypress', function (e) {
if (e.which == 13) {
e.preventDefault();
var $next = $('[tabIndex=' + (+this.tabIndex + 1) + ']');
console.log($next.length);
if (!$next.length) {
$next = $('[tabIndex=1]');
}
$next.focus();
}
});
Run Code Online (Sandbox Code Playgroud)
$(document).on('keypress', 'input,select', function (e) {
if (e.which == 13) {
e.preventDefault();
var $next = $('[tabIndex=' + (+this.tabIndex + 1) + ']');
console.log($next.length);
if (!$next.length) {
$next = $('[tabIndex=1]');
}
$next.focus();
}
});
Run Code Online (Sandbox Code Playgroud)
病态创建了非jQuery版本。所以只有纯Javascript; https://jsfiddle.net/mm0uctuv/2/
Javascript:
var inputs = document.querySelectorAll("input,select");
for (var i = 0 ; i < inputs.length; i++) {
inputs[i].addEventListener("keypress", function(e){
if (e.which == 13) {
e.preventDefault();
var nextInput = document.querySelectorAll('[tabIndex="' + (this.tabIndex + 1) + '"]');
if (nextInput.length === 0) {
nextInput = document.querySelectorAll('[tabIndex="1"]');
}
nextInput[0].focus();
}
})
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<form>
Field 1: <input type="text" tabindex="1"><br>
Field 3: <input type="text" tabindex="3"><br>
Field 2: <input type="text" tabindex="2">
</form>
Run Code Online (Sandbox Code Playgroud)