我正在使用带触摸屏的 jQuery 屏幕键盘插件。 在我的输入中,我想在达到 4 个字符时关闭键盘。 不幸的是,键盘只知道我何时达到 4 个字符,并且可以将最大输入限制为 4 个字符。 用户仍然需要手动关闭键盘。 有关闭键盘的密码吗? 这是我的实际脚本:
$('#password').keyboard({
layout: 'custom',
customLayout: {
'default' : [
'C D E F',
'8 9 A B',
'4 5 6 7',
'0 1 2 3',
'{bksp} {a} {c}'
]
},
usePreview : false,
autoAccept: true,
maxLength : 4,
// Prevent keys not in the displayed keyboard from being typed in
restrictInput : true,
// include lower case characters (added v1.25.7)
restrictInclude : 'a b c d e f',
// don't use combos or A+E could become a ligature
useCombos : false,
// activate the "validate" callback function
acceptValid : true,
validate : function(keyboard, value, isClosing){
// only make valid if input is 4 characters in length
if(value.length === 4)
return true; // I want to close the keyboard here
return false;
}
});
最佳答案
最初,我建议在 change
回调中添加 setTimeout
,但后来我注意到由于键盘正在关闭并且“keyup”而弹出了 JavaScript 错误事件仍在触发。
无论如何,我修复了这些错误并添加了新的 autoAcceptOnValid
option - 按如下方式使用它( demo ):
$(function() {
$('#password').keyboard({
layout: 'custom',
customLayout: {
'default': [
'C D E F',
'8 9 A B',
'4 5 6 7',
'0 1 2 3',
'{bksp} {a} {c}'
]
},
usePreview: false,
autoAccept: true,
maxLength: 4,
// Prevent keys not in the displayed keyboard from being typed in
restrictInput: true,
// include lower case characters (added v1.25.7)
restrictInclude: 'a b c d e f',
// don't use combos or A+E could become a ligature
useCombos: false,
// activate the "validate" callback function
acceptValid: true,
// option added in v1.25.29
autoAcceptOnValid: true,
validate: function(keyboard, value, isClosing) {
return value.length === 4;
}
});
});
关于jquery - Mottie 的虚拟键盘接近验证真实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515882/