javascript - 按下一步按钮时选择文本框值

标签 javascript jquery

我有一个文本框网格。当用户按下箭头按钮时,我需要光标按照他们刚刚按下的方向选择文本框的内容。我尝试过使用 JQuery 的 .select() 方法,但这似乎在 IE 中不起作用。

您能给我一些关于如何让它发挥作用的建议吗?

提前致谢。

最佳答案

有人可以修复这里的左右吗? 上下移动就可以了,在文本框中导航似乎优先

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>test tab</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script type="text/javascript">
function checkKey(e){
     switch (e.keyCode) {
        case 40:
            current = $("#field1"); 
            $("#msg").html(current.id); 
            setTimeout('focusIt()',100);
            break;
        case 38:
            current = $("#field3"); // or some more clever way of finding the last field
            $("#msg").html(current.id); 
            setTimeout('focusIt()',100);
            break;
        case 37:
            if (prev) current = prev;
            else current = $("#field3"); // or some more clever way of finding the last field
            $("#msg").html(current.id); 
            setTimeout('focusIt()',100);
            break;
        case 39:
            if (next) current = next; 
            else current = $("#field1")
            $("#msg").html(current.id); 
            setTimeout('focusIt()',100);
            break;
        default:

            }      
}
function focusIt() {
  if(current) {current.focus(); current.select() }
}
var current,next,prev;
$(document).ready(function() {
  if ($.browser.mozilla) {
    $("input").keypress (checkKey);
  } else {
    $("input").keydown (checkKey);
  }
  $("input").focus(function() {
    current=this;
    $("#msg").html(current.id);
    next = $("field"+(parseInt(current.id.replace('field',''))+1));
    prev = $("field"+(parseInt(current.id.replace('field',''))-1));
  });
});
</script>
</head>
<body>
<div id="pagecontent" style="width:95%; margin:10px 10px 10px 10px">
   <form>
   <input type="text" id="field1" value="first"/><input type="text" id="field2" value="second"/><input type="text" id="field3" value="third"/>
   </form>
   <span id="msg"></span>   
</div>
</body>
</html>

关于javascript - 按下一步按钮时选择文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3185120/

相关文章:

javascript - 速度自动选择一个值

javascript - SailsJs 将函数调用到同一个 Controller 中

javascript - Java中如何通过request get参数获取html按钮的id?

javascript - 通过jquery在数据表中设置 "Search Box"的值为空

javascript - AngularJS 中的展开折叠按钮

javascript - 如何在点击时和用户点击离开时使用 jquery 隐藏元素?

jquery - 修复了 jQuery Mobile 表中的 thead

javascript - 代码之前的jquery addClass 和代码之后的removeClass 不起作用

javascript - 光标出现在搜索框上点击

javascript - 通过重叠的 svg 元素传播事件