2017-03-13 2 views
0

Ich habe eine autotab Funktion,autotabulate andernfalls mit IE

<input type="text" class="autoTab" maxlength="2" /> : 
<input type="text" /> 

autoTab : function(){ 
    $('.autoTab').on('keypress', function(e){ 

    if($(this).val().length + 1 === parseInt($(this).attr("maxlength"))){ 
    $(this).next().focus(); 
    } 
}); 

}

es funktioniert gut in Chrome, aber in IE, tabellarisch vor Schreib de letzte Zeichen, und es wird nicht geschrieben.

Ich versuchte dies:

autoTab : function(){ 
    $('.autoTab').on('keypress', function(e){ 

     var ua = window.navigator.userAgent; 
     var msie = ua.indexOf("MSIE "); 
     var isIE = (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)); 

    if((!isIE && $(this).val().length + 1 === parseInt($(this).attr("maxlength"))) || (isIE && $(this).val().length === parseInt($(this).attr("maxlength")))) { 
     $(this).next().focus(); 
    } 
    }); 
} 

es funktioniert immer noch in Chrome, aber in IE, ich schreibe 2 Zeichen und der Cursor weiter in der Box.

Wie kann ich es beheben?

Antwort

1

Im Allgemeinen ist das Problem, dass $ (this) .val(). Length + 1 true auswertet, und Sie sind hart-Vergleich mit int ... für Internet Explorer, dass wenn immer wahr ist ... als alle Zahl> 0 ist wahr. Irgendwie komisches Verhalten. Würde es als einen Fehler betrachten.

Noch einfache Möglichkeit, es zu beheben:

HTML:

<input type="text" class="autoTab" maxlength="2" /> : 
<input type="text" /> 

JS:

$('.autoTab').on('keyup', function(e){ 
if($(this).val().length == parseInt($(this).attr("maxlength"))){ 
    $(this).next().focus(); 
    } 
}); 

Dies funktioniert gut auf Chrome, Opera und Internet Explorer.

Live example

Bitte beachten Sie, dass ich ===-== in if Anweisung geändert und umge Ereignis onkeyup.

Verwandte Themen