[英]Mottie Keyboard: Change focus when input field equals maxlength
如果输入字段等于maxlength,我将使输入自动将焦点更改为下一个输入。 输入字段与Mottie Keyboard集成在一起。 是否有可能做到这一点?
这是演示: JSFIDDLE
如果不使用虚拟键盘,则使用此代码很容易:
$("input").bind("input", function() {
var $this = $(this);
setTimeout(function() {
if ( $this.val().length >= parseInt($this.attr("maxlength"),10) )
$this.next("input").focus();
},0);
});
当我将上面给出的脚本与Mottie Keyboard结合使用时,它将无法正常工作。
我认为您还在存储库中打开了该问题 ?
要在此处总结我的响应,请使用带有switchInput
API函数的change
回调来完成所需的操作( 演示 ):
HTML(示例)
<input type="text" /> <!-- max len = 3 -->
<input type="text" /> <!-- max len = 3 -->
<input class="last" type="text" /> <!-- max len = 4 -->
脚本
$(function() {
$("input").keyboard({
position: {
// position under center input
of: $("input:eq(1)"),
// move down 12px; not sure why it doesn't line up
my: 'center top+12',
at: 'center top'
},
enterNavigation: true,
maxLength: 4,
layout: 'num',
autoAccept: true,
usePreview: false,
change: function(e, keyboard, el) {
var len = keyboard.$el.hasClass("last") ? 4 : 3;
if (keyboard.$el.val().length >= len) {
// switchInput( goToNext, isAccepted );
keyboard.switchInput(true, true);
} else if (keyboard.$el.val() === "" && keyboard.last.key === "bksp") {
// go to previous if user hits backspace on an empty input
keyboard.switchInput(false, true);
}
}
});
});
尝试像这样使用,希望它能起作用
$("input").on("focus blur", function() {
var $this = $(this),
value = $this.val(),
maxLength = $this.attr("maxlength");
if ( value.length >= maxLength ){
$this.next("input").focus();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.