JS实现表格使用上下左右键聚集
2015-10-06 16:01
531 查看
//调用:new tabTableInput("tblGrid","text"); var tabTableInput = function (tableId, inputType) { var rowInputs = []; var trs = $("#" + tableId).find("tr"); var inputRowIndex = 0; $.each(trs, function (i, obj) { if ($(obj).find("th").length > 0) { //跳过表头 return true; } var rowArray = []; var thisRowInputs; if (!inputType) { //所有的input thisRowInputs = $(obj).find("input:not(:disabled):not(:hidden):not([readonly])"); } else { thisRowInputs = $(obj).find("input:not(:disabled):not(:hidden):not([readonly])[type=" + inputType + "]"); } if (thisRowInputs.length == 0) return true; thisRowInputs.each(function (j) { $(this).attr("_r_", inputRowIndex).attr("_c_", j); rowArray.push({ "c": j, "input": this }); $(this).keydown(function (evt) { var r = $(this).attr("_r_"); var c = $(this).attr("_c_"); if (evt.which < 37 || evt.which > 40) { return; } var tRow if (evt.which == 38) { //上 if (r == 0) return; r--; //向上一行 tRow = rowInputs[r]; if (c > tRow.length - 1) { c = tRow.length - 1; } } else if (evt.which == 40) { //下 if (r == rowInputs.length - 1) { //已经是最后一行 return; } r++; tRow = rowInputs[r]; if (c > tRow.length - 1) { c = tRow.length - 1; } } else if (evt.which == 37) { //左 if (r == 0 && c == 0) { //第一行第一个,则不执行操作 return; } if (c == 0) { //某行的第一个,则要跳到上一行的最后一个,此处保证了r大于0 r--; tRow = rowInputs[r]; c = tRow.length - 1; } else { //否则只需向左走一个 c--; } } else if (evt.which == 39) { //右 tRow = rowInputs[r]; if (r == rowInputs.length - 1 && c == tRow.length - 1) { //最后一个不执行操作 return; } if (c == tRow.length - 1) { //当前行的最后一个,跳入下一行的第一个 r++; c = 0; } else { c++; } } $(rowInputs[r].data[c].input).focus().select(); }); }); rowInputs.push({ "length": thisRowInputs.length, "rowindex": inputRowIndex, "data": rowArray }); inputRowIndex++; }); }
相关文章推荐
- Jsp的定义及作用还有声明
- JS-HTML DOM remove() 方法
- JS短路原理的应用示例 精简代码的途径
- JavaScript编程题
- JS-得到屏幕宽高、页面宽高
- turnjs4翻书效果
- JavaScript Array 对象splice() 方法
- 5个典型的JavaScript面试题
- JavaScript,base64加密解密
- JS打印指定内容
- 拥抱javascript的promise
- 如何用javascript 的eval动态执行一个需要传对象参数的函数
- JS-为句柄添加监听函数
- Jsp2.0环境下的excel下载开发
- JSP实现快速上传文件的方法
- Jsp+Servlet实现文件上传下载——前台页面开发
- 悟透JavaScript
- .NET-DataTable或DataSet转JSON
- JS实现三个层重叠点击互相切换的方法
- js中if判断表达式