2016-09-21 35 views
0

如果輸入字段等於maxlength,我將使輸入自動將焦點更改爲下一個輸入。輸入字段與Mottie鍵盤集成。是否有可能做到這一點?Mottie鍵盤:當輸入字段等於最大長度時更改焦點

這裏的DEMO: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

我認爲你沒有將任何事件綁定到輸入字段。另外,如果時間爲0,則不需要調用'setTimeout()'函數。 – Samir

+0

你可以給我jsfiddle演示嗎? @薩米爾 –

回答

2

我想你也打開資源庫中的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); 
     } 
    } 
    }); 
}); 
+0

我已經[更新了你的演示以及](http://jsfiddle.net/Mottie/MK947/4437/)...我不得不調整一個很少有事情能夠按預期工作。 – Mottie

+0

好的,謝謝你兄弟,並抱歉重複的問題:) –

+0

不能使用alwaysOpen? –

0

嘗試使用這樣的,希望這將工作

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

不工作的傢伙 –

+0

嘗試使用'focus blur'方法。 – Samir

+0

我已經變更爲'focus&blur',但它也一樣 –

相關問題