2

我在一个最大长度为 4 的页面上有四个文本框。我的目标是当用户填写第一个文本框时,焦点会自动移动到另一个文本框。我编写的方法在所有网络浏览器中都可以正常工作,但在移动浏览器中却不行。

我正在使用的标记:

<input tabindex="3" type="text" id="cnumber1" class="inputBx ccNumber" style="width:57px;margin-left: 11px!important" maxlength="4" size="4" autocomplete="off" />
        <input tabindex="4" type="text" id="cnumber2" class="ccNumber" style="width:57px;"  maxlength="4" size="4" autocomplete="off"/>
        <input tabindex="5" type="text" id="cnumber3" class="ccNumber" style="width:56px;"  maxlength="4" size="4" autocomplete="off"/>
        <input tabindex="6" type="text" id="cnumber4" class="ccNumber" style="width:56px;"  maxlength="4" size="4" autocomplete="off"/>

我正在使用的 Jquery 方法:

(function($) {
  $.switchFocus = function($this) {
    $this.unbind('keyup');
    $this.each(function() {
      $(this).keyup(function(){
        var $this = $(this),
        inputVal = $this.val(),
        iLen = inputVal.length,
        $nextEle = $this.next();
        if (iLen == $this.attr("size") && $nextEle.length > 0){
          if($nextEle.val().length === 0)
            $nextEle.focus();
        }
      });
    });
  }
}(jQuery));

我可以就移动浏览器为什么会发生这种情况获得一些帮助吗?

4

1 回答 1

0

一定是部分手机浏览器的bug,不过这个方法应该能解决你的问题,试试看,

(function($) {
  $.switchFocus = function($this) {
    $this.unbind('keyup');
    $this.each(function() {
      $(this).keyup(function(){
        var $this = $(this),
        inputVal = $this.val(),
        iLen = inputVal.length,
        $nextEle = $this.next();
        if (iLen == $this.attr("size") && $nextEle.length > 0){
          if($nextEle.val().length === 0)
            $nextEle.focus();
            setTimeout(function(){$nextEle.focus()},0);
        }
      });
    });
  }
}(jQuery));

演示

于 2012-07-31T13:57:07.397 回答