我試圖在使用jQuery的多個輸入字段之間構建一個簡單的導航機制。代碼的第一部分使用向下箭頭或返回鍵跳過,但當我通過查找向上箭頭然後顛倒順序添加第二個塊後退時,輸入第一個文本字段向右跳走到第二。有什麼想法嗎?使用箭頭鍵瀏覽文本輸入字段並返回
<script type="text/javascript">
$(document).ready(function(){
// get only input tags with class data-entry
textboxes = $("input.data-entry");
// now we check to see which browser is being used
if ($.browser.mozilla) {
$(textboxes).keypress (checkForAction);
} else {
$(textboxes).keydown (checkForAction);
}
});
function checkForAction (event) {
if (event.keyCode == 13 || 40) {
currentBoxNumber = textboxes.index(this);
if (textboxes[currentBoxNumber + 1] != null) {
nextBox = textboxes[currentBoxNumber + 1]
nextBox.focus();
nextBox.select();
event.preventDefault();
return false;
}
}
if (event.keyCode == 38) {
currentBoxNumber = textboxes.index(this);
if (textboxes[currentBoxNumber - 1] != null) {
prevBox = textboxes[currentBoxNumber - 1]
prevBox.focus();
prevBox.select();
event.preventDefault();
return false;
}
}
}
</script>
爲什麼這樣做呢?在每個支持表單的瀏覽器中,* tab *會通過* tabIndex *轉到下一個控件,* shift + tab *返回。沒有腳本,沒有瀏覽器嗅探,沼澤標準的用戶界面,所以沒有用戶的驚喜。 – RobG