• 用上下左右箭头键在textbox中的光标跳转


    How to allow up and down arrows to navigate your form fields
    October 5th, 2008 by Robert

    I learned something pretty cool last week. I fielded a request from a client who wanted to be able to use the up and down arrows on his keyboard to navigate a rather large form on the site I created for him. I believed it was possible but I had never done it. So after doing some research I figured out how to do it. Below is a simplistic version (put your cursor in one of the text boxes below and push the up or down arrow keys to move between them):

    1-1 1-2 1-3
    2-1 2-2 2-3
    3-1 3-2 3-3

    The HTML for the simple table is as follows:

    01.
    02.
    03.1-1
    04.
    05.1-2
    06.
    07.1-3
    08.
    09.
    10.
    11.2-1
    12.
    13.2-2
    14.
    15.2-3
    16.
    17.
    18.
    19.3-1
    20.
    21.3-2
    22.
    23.3-3
    24.
    25.
    26.

    The javascript function used is as follows:

    01.
    02.function checkKey(event, numRecs, numCols, myName, myRow, myCol) {
    03.var key = event.keyCode;
    04.var bSetFocus = false;
    05.var myId = '';
    06.// if the left arrow has been pressed and we're not in the first column
    07.if( key == 37) {
    08.if (myCol-1 > 0) {
    09.myId =  myName + myRow + (myCol-1);
    10.bSetFocus = true;
    11.}
    12.}
    13.// if the up arrow has been pressed and we're not in the first row
    14.if( key == 38) {
    15.if (myRow-1 > 0) {
    16.myId =  myName + (myRow-1) + myCol;
    17.bSetFocus = true;
    18.}
    19.}
    20.// if the right arrow has been pressed and we're not in the last column
    21.if( key == 39) {
    22.if (myCol+1
    23.myId =  myName + myRow + (myCol+1);
    24.bSetFocus = true;
    25.}
    26.}
    27.// if the down arrow has been pressed and we're not in the last row
    28.else if( key == 40 ) {
    29.if (myRow+1
    30.myId =  myName + (myRow+1) + myCol;
    31.bSetFocus = true;
    32.}
    33.}
    34.if (bSetFocus) document.getElementById(myId).focus();
    35.} // end checkKey function
    36.// -->

    In my code I generate the HTML using PHP so when I pass the number of records to the checkKey function (3 in this example) it is the number of rows returned from a database query, and the current row for any given row of the table is the counter in a loop I use to print the table.

    By the way, if you want to allow for the left and right arrows to move left and right in a form, you can do that by expanding the checkKey function to look for key code 37 (left arrow) and key code 39 (right arrow), and you’ll need to implement a column ordering scheme (like the counter I used for the rows) so you’ll know where to take the cursor.

    Four Direction Code
    (submitted by Mr Arrows below, but the comment fields do not accept code so I put it in the main post. I actually had trouble putting two working code samples in the same post so I just updated the main example above to include the left and right arrows)

    What if there are values in each field?

    1. These two javascript functions need to be defined, which will tell you the index of the start of the current selection and the end of the current selection (which will be the same if nothing is selected and you just have a blinking cursor in the input box). They both take as input the same object (called o below) that is passed into checkKey as the last param (called myObj there):
      01.function getSelectionStart(o) {
      02.if (o.createTextRange) {
      03.var r = document.selection.createRange().duplicate();
      04.r.moveEnd('character', o.value.length);
      05.if (r.text == '') return o.value.length;
      06.return o.value.lastIndexOf(r.text);
      07.} else return o.selectionStart;
      08.}
      09. 
      10.function getSelectionEnd(o) {
      11.if (o.createTextRange) {
      12.var r = document.selection.createRange().duplicate();
      13.r.moveStart('character', -o.value.length);
      14.return r.text.length;
      15.} else return o.selectionEnd;
      16.}
    2. Then you need to define some variables inside the checkKey function, to get the current input value and the index of the cursor position within that value:
      1.var inputVal = myObj.value,
      2.startIndex = getSelectionStart(myObj);
      3.endIndex = getSelectionEnd(myObj);

      I only use startIndex because it will be the same as endIndex when nothing is highlighted or selected in the input box.

    3. Next you can use a simple regular expression (or any other string function you want) to see if there is any text before (for the left arrow condition) or after (for the right arrow condition) the cursor. For example, this is the code I use when when the left arrow has been pressed:
      01.var testBC = inputVal.substring(0, startIndex);
      02.var blankRE=/^s*$/;
      03.//alert("beforeCursor is '"+testBC+"');
      04.// if the regular expression test passes, then there is nothing to the left and we can move cells
      05.if (blankRE.test(testBC)) {
      06.var nextField = document.getElementById(columns[colIndex-1]+String(myRow));
      07.nextField.focus();
      08.//If we are moving into a select dropdown, restore the previously selected value half a second after it switches to the first or last option (I don't know how to prevent that, so this is my workaround):
      09.if (nextField.tagName=="SELECT") {
      10.var cur_ind = nextField.options.selectedIndex;
      11.var cur_val = nextField.options[nextField.options.selectedIndex].value;
      12.var cur_text = nextField.options[nextField.options.selectedIndex].text;
      13.setTimeout(function(){nextField.options.selectedIndex=cur_ind}, 5);
      14.}
      15.return false;
      16.}






    +++++++++++++++++++++++++++++++

    兄弟作一录入界面,里面有很多行textbox,每行5个,为录入方便(有的textbox可能这次不录入,数据为0),要求实现用上下左右箭头键在textbox中实现光标的快速跳转,即敲下箭头键光标就直接跳入下一行对应的textbox,其余上、左、右以此类推。

    请问各位大哥如何实现?
    +++++++++++++++++++++++++++++++
    try   something   like   the   following:











    <script   language=javascript>
    var   n   =   4;
    function   document.onkeydown()
    {
        //alert(event.keyCode);

        var   e   =   event.srcElement;
        var   c   =   event.keyCode;
        switch(c)
        {
              case   37:   //left
    moveCursor(e,-1);
    break;

              case   38:   //up
    moveCursor(e,-n);
    break;

              case   39:   //right

    moveCursor(e,1);
    break;

              case   40:   //down
    moveCursor(e,n);
    break;

        }
    }

    function   moveCursor(e,n)
    {
        var   i   =   e.sourceIndex;
          var   sign   =   n   >   0?   1   :   -1;
          n   =   Math.abs(n);

          i=i+sign*1;

          while   (   i   > =0   &&   i   <   document.all.length)
          {
    e   =   document.all[i];
    if   (e.tagName   ==   "INPUT "   &&   e.type   ==   "text ")
    {
    n--;
    if   (n==0)
    break;
    }
    i=i+sign*1;
          }
          if   (   i   > =   0   &&   i   <   document.all.length   &&   n==0)
    document.all[i].focus();

    }
    </script>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
    阅读(1286) | 评论(0) | 转发(0) |
    0

    上一篇:创建网站地图

    下一篇:SHELL中时间的比较

    给主人留下些什么吧!~~
    评论热议
  • 相关阅读:
    点击表单提交 却打开了另存为对话框
    js中对象的复制,浅复制(浅拷贝)和深复制(深拷贝)
    html怎么隐藏input标签
    莫名其妙地改变了数组的值,原来是……
    addEventListener 用法
    JavaScript DOM元素常见操作详解【添加、删除、修改等】
    MySQL用户权限验证与管理方法详解
    mysql如何修改root用户的密码
    python案例
    IDEA启动报 Error:java: JDK isn't specified for module 'xxx' 已解决
  • 原文地址:https://www.cnblogs.com/ztguang/p/12647355.html
Copyright © 2020-2023  润新知