【问题标题】:Mottie Keyboard: Change focus when input field equals maxlengthMottie 键盘:当输入字段等于 maxlength 时更改焦点
【发布时间】:2017-01-29 10:07:35
【问题描述】:

如果输入字段等于 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 键盘结合使用时,它不起作用。

【问题讨论】:

  • 我认为您没有将任何事件绑定到输入字段。另外,如果时间为0,则无需调用setTimeout()函数。
  • 你能给我jsfiddle演示吗? @萨米尔

标签: javascript jquery keyboard virtual-keyboard


【解决方案1】:

我想你也打开了存储库中的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);
      }
    }
  });
});

【讨论】:

  • 我已经 updated your demo as well... 我必须调整一些东西才能让它按预期工作。
  • 好的,谢谢兄弟,抱歉重复的问题:)
  • 不能使用alwaysOpen?
【解决方案2】:

尝试这样使用,希望这会奏效

$("input").on("focus blur", function() {
    var $this = $(this),
        value = $this.val(),
        maxLength = $this.attr("maxlength");
    if ( value.length >= maxLength  ){
       $this.next("input").focus();
    }      
});

【讨论】:

  • 尝试使用focus blur 方法。
  • 我已经改成focus &amp; blur,但还是一样
  • 这适用于任何移动设备吗?还是用于桌面网络应用程序?
  • 桌面网络兄弟,你可以在我对示例的回答中看到我的 jsfiddle
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 2019-03-31
相关资源
最近更新 更多