Javascript 自动配对,IE失败
我有自动标签功能Javascript 自动配对,IE失败,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,我有自动标签功能 <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"))){ $(thi
<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中,我写了2个字符,光标继续在框中
如何修复它?通常问题是$(this).val().length+1的计算结果为
true
,您很难将其与int进行比较。。。对于Internet Explorer,如果始终为真。。。因为任何大于0的数字都是真的。有点奇怪的行为。我会认为这是一个bug
仍然是修复它的简单方法:
HTML:
这适用于Chrome、Opera和IE
请注意,我在if
语句中将===
更改为=
,并将事件切换为onkeyup
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();
}
});
}
<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();
}
});