+ -
当前位置:首页 → 问答吧 → jquery 获取焦点在文本之后问题

jquery 获取焦点在文本之后问题

时间:2011-12-12

来源:互联网

HTML code

 $("input:text").each(function () {                                  // 循环表单中所有的 text
            $(this).keyup(function () {                        
                if (event.keyCode == 13) {                           //当按回车键触发事件
                    var tt = $("form[name='why']").find(":text");     //找到所有的text 
                    var idx = tt.index(this);                          //得到当前文本框的 索引
                    if (idx != tt.length - 1) {                        //判断 如果 不是最后一个索引
                        tt[idx+1].focus();                             // 下一个文本框得到焦点
                                                  
                    }
                }
            });



不确定有多少个文本框 文本框 也不是相连的 所以我这样做 问题就在于 下一个文本框得到焦点不是在文本的后面 我想得到的焦点在文本的后面 各位大大帮帮忙。。。

作者: bianchenga   发布时间: 2011-12-12

坐等大牛啊!!!!!!

作者: bianchenga   发布时间: 2011-12-12

http://topic.csdn.net/u/20111207/17/c68971da-d1d4-4b6f-a449-0d4361f23f7c.html

看这个帖子

作者: a67251026   发布时间: 2011-12-12

蛋疼哥 上次用你也的 那个 你忽略一个问题 就是 所有的文本框不一定是连接的 也就是说 两个text之间有别的 控件 就不好用了

作者: bianchenga   发布时间: 2011-12-12

JScript code



$("input:text").each(function () {                                  // 循环表单中所有的 text
            $(this).keyup(function () {                        
                if (event.keyCode == 13) {                           //当按回车键触发事件
                    var tt = $("form[name='why']").find(":text");     //找到所有的text 
                    var idx = tt.index(this);                          //得到当前文本框的 索引
                    if (idx != tt.length - 1) {                        //判断 如果 不是最后一个索引
                   tt[idx+1].focusEnd();

                           // 下一个文本框得到焦点
                                                  
                    }
                }
            });





 $.fn.setCursorPosition = function(position) {
          if (this.lengh == 0) return this;
          return $(this).setSelection(position, position);
      }

      $.fn.setSelection = function(selectionStart, selectionEnd) {
          if (this.lengh == 0) return this;
          input = this[0];

          if (input.createTextRange) {
              var range = input.createTextRange();
              range.collapse(true);
              range.moveEnd('character', selectionEnd);
              range.moveStart('character', selectionStart);
              range.select();
          } else if (input.setSelectionRange) {
              input.focus();
              input.setSelectionRange(selectionStart, selectionEnd);
          }

          return this;
      }

      $.fn.focusEnd = function() {
          this.setCursorPosition(this.val().length);
      }






这样不就好了

作者: a67251026   发布时间: 2011-12-12

试过啦 蛋疼哥 tt[idx+1] 只是虚拟的找到了文本框 用不了 你写的.focusEnd ()方法 
要是$("#ID") 是可以的 帮想想办法 蛋疼哥

作者: bianchenga   发布时间: 2011-12-12

求高手啊

作者: bianchenga   发布时间: 2011-12-12

var str = tt[idx + 1].id;
  $("#" + str).focusEnd();

作者: a67251026   发布时间: 2011-12-12