0
在某些jQuery中遇到了一些问题。我试图实现类似于Excel导航的数据输入网格,使用箭头上下遍历列。我有负载运行的脚本来为每个文本框分配rel属性和唯一ID,如下所示:jQuery在keydown的texbox中选择文本
id="gScore_0" class="gScoreTB" rel=0
id="gScore_1" class="gScoreTB" rel=1
id="gScore_2" class="gScoreTB" rel=2
...等等。然后,我有以下处理程序:
$('.gScoreTB').keydown(function (e) {
var keyCode = e.keyCode || e.which,
arrow = { left: 37, up: 38, right: 39, down: 40 };
switch (keyCode) {
case arrow.up:
var str = $(this).attr('rel');
str = parseInt(str) - 1;
$("#gScore_" + str).focus();
break;
case arrow.down:
//Set focus to the same cell next row
var str = $(this).attr('rel');
str = parseInt(str) + 1;
$("#gScore_" + str).focus();
break;
}
});
的理论是,函数添加/从ID中减1,然后将焦点到该文本框。这一切都是伟大的枪支。
问题是我想重点改变和框中的任何文字都被选中。除了.focus()
之外,我尝试过在文本框中使用.select()
的各种组合,但没有发生。我怀疑这与事件有关,但我不是jQuery天才。请帮忙!由于
PS如果有人认为我所做的事情是荒谬的,我始终批评/改进:-)
我看到的一个改进是你如何获得keyCode - 你不需要检查keyCode和哪个 - jQuery规范化,所以你不必显式检查 - 只需使用“e.which”,它将永远是正确的。另外,对于选择文本,请尝试$(“selector”)[0] .focus(); $( “same_selector”)[0]。选择(); – Ian 2013-05-01 03:39:27