Mottie的虚拟键盘关闭验证是真的

时间:2016-05-29 23:24:26

标签: jquery jquery-plugins virtual-keyboard

我正在使用带触摸屏的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;
        }
    });

1 个答案:

答案 0 :(得分:1)

最初,我打算建议在setTimeout回调中添加change,但后来我注意到javascript错误弹出,因为键盘正在关闭且“keyup”事件仍在触发。 / p>

无论如何,我修正了这些错误并添加了新的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;
    }
  });

});