首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动计数,IE失败

自动计数,IE失败
EN

Stack Overflow用户
提问于 2017-03-13 09:10:28
回答 1查看 20关注 0票数 0

我有一个自动标签函数,

代码语言:javascript
复制
 <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中,在写最后一个字符之前,它是表格的,而且它不是写的。

我试过这个:

代码语言:javascript
复制
    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个字符,光标继续在框中。

我怎么才能修好它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-13 09:21:55

通常问题是$(this).val().length +1的计算结果为true,您很难将它与int.对于来说,如果是真的.因为任何数字>0都是真的。有点奇怪的行为。会把它当成虫子。

修复它的方法仍然很简单:

HTML:

代码语言:javascript
复制
<input type="text" class="autoTab" maxlength="2" /> :
<input type="text" />

联署材料:

代码语言:javascript
复制
$('.autoTab').on('keyup', function(e){
 if($(this).val().length == parseInt($(this).attr("maxlength"))){
     $(this).next().focus();
  }
});

这在Chrome,Opera和IE上运行得很好。

实例化

请注意,我在===语句中将==更改为==,并将事件切换为onkeyup

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42759920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档