Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按下下一步按钮时选择文本框值_Javascript_Jquery - Fatal编程技术网

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

Javascript 按下下一步按钮时选择文本框值,javascript,jquery,Javascript,Jquery,我有一个网格的文本框。当用户按下箭头按钮时,我需要光标按他们刚刚按下的方向选择文本框的内容。我尝试过使用JQuery的.select方法,但这在IE中似乎不起作用 你能给我一些建议如何让它工作吗 提前感谢。只需使用文本框的选择方法,该方法适用于所有主要的可编写脚本的浏览器,只要您先将其聚焦: <input type="text" id="aTextBox" value="Some text"> <script type="text/javascript"> var

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

你能给我一些建议如何让它工作吗

提前感谢。

只需使用文本框的选择方法,该方法适用于所有主要的可编写脚本的浏览器,只要您先将其聚焦:

<input type="text" id="aTextBox" value="Some text">
<script type="text/javascript">
    var textBox = document.getElementById("aTextBox");
    textBox.focus();
    textBox.select();
</script>

有人能修一下这里的左右两边吗? 在文本框中导航似乎要先走一步

<!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>

试着在一行中使用这两个键

我发现:对,按箭头键时,很好。我可能从问题中错误地推断出这不是问题,而是偶然的。问题似乎出在选择上:我尝试过使用JQuery的.select方法,但这在IE中似乎不起作用。Tim你是对的。问题是在文本框中选择文本。按箭头是偶然的。我试着提供一些关于我正在做的事情的信息。谢谢罗伯特,我在一条线上试过了,但似乎不起作用。
<input type="text" id="aTextBox" value="Some text">
<script type="text/javascript">
    $('#TextBox').focus().select();
</script>