Mottie 键盘:当输入字段等于最大长度时更改焦点
Mottie Keyboard: Change focus when input field equals maxlength
如果输入字段等于最大长度,我将使输入自动将焦点切换到下一个输入。输入字段与 Mottie 键盘集成。可以这样做吗?
这是演示: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 键盘结合使用时,它不起作用。
尝试这样使用,希望这会起作用
$("input").on("focus blur", function() {
var $this = $(this),
value = $this.val(),
maxLength = $this.attr("maxlength");
if ( value.length >= maxLength ){
$this.next("input").focus();
}
});
我想你也打开了存储库中的 issue?
总结一下我的回复,使用 change
回调和 switchInput
API 函数来完成你需要的(demo):
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);
}
}
});
});
如果输入字段等于最大长度,我将使输入自动将焦点切换到下一个输入。输入字段与 Mottie 键盘集成。可以这样做吗?
这是演示: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 键盘结合使用时,它不起作用。
尝试这样使用,希望这会起作用
$("input").on("focus blur", function() {
var $this = $(this),
value = $this.val(),
maxLength = $this.attr("maxlength");
if ( value.length >= maxLength ){
$this.next("input").focus();
}
});
我想你也打开了存储库中的 issue?
总结一下我的回复,使用 change
回调和 switchInput
API 函数来完成你需要的(demo):
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);
}
}
});
});