我有一个自动标签函数,
<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();
}
});}
它在Chrome中工作得很好,但是在IE中,在写最后一个字符之前,它是表格的,而且它不是写的。
我试过这个:
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();
}
});
}它仍然工作在Chrome,但在IE中,我写了2个字符,光标继续在框中。
我怎么才能修好它?
发布于 2017-03-13 09:21:55
通常问题是$(this).val().length +1的计算结果为true,您很难将它与int.对于来说,如果是真的.因为任何数字>0都是真的。有点奇怪的行为。会把它当成虫子。
修复它的方法仍然很简单:
HTML:
<input type="text" class="autoTab" maxlength="2" /> :
<input type="text" />联署材料:
$('.autoTab').on('keyup', function(e){
if($(this).val().length == parseInt($(this).attr("maxlength"))){
$(this).next().focus();
}
});这在Chrome,Opera和IE上运行得很好。
请注意,我在===语句中将==更改为==,并将事件切换为onkeyup。
https://stackoverflow.com/questions/42759920
复制相似问题