Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6684565
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: 系统运维

2011-11-08 18:50:56


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.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
1-11-21-3
2-12-22-3
3-13-23-3

The javascript function used is as follows:

01.

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:












阅读(3220) | 评论(0) | 转发(0) |
0

上一篇:创建网站地图

下一篇:SHELL中时间的比较

给主人留下些什么吧!~~