我有一个文本框网格。当用户按下箭头按钮时,我需要光标按照他们刚刚按下的方向选择文本框的内容。我尝试过使用 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/