[英]navigate form fields and edit text with the arrow keys
我有一個網站形式的腳本,按下回車鍵或右箭頭將焦點更改為下一個字段,或者按向左箭頭將焦點更改為前一個字段。
我需要能夠在編輯文本時使用箭頭鍵更改光標位置,而不更改字段焦點,只有當光標位於文本的開頭或結尾時才轉到下一個字段。
我怎么能做到這一點?
腳本代碼如下:
function tabulacion_manual(e,adelante,atras,saltar){
if(saltar==1)
{
setTimeout(function(){
$(adelante).focus();
$(adelante).select();
},150);
}else{
//tecla = (document.all) ? e.keyCode : e.which;
tecla = (window.event) ? window.event.keyCode : e.keyCode;
switch(tecla){
case 37: //atras
$(atras).focus();
break;
case 38: //arriba
$(atras).focus();
break;
case 40: //abajo
$(adelante).focus();
break;
case 39: //derecha
$(adelante).focus();
break;
case 13: //enter
$(adelante).focus();
break;
}
}
return true;
}
HTML代碼是:
<s:textfield name="programaFaena.fecha" maxlength="10" tabindex="1" onkeypress="return tabulacion_manual(event,this,next())" autocomplete="off" readonly="false" >
例如:
case 37: //atras
//if the cursor is at the beginning of the field
if (event.target.selectionEnd == 0) {
$(atras).focus();
}
break;
...
case 39: //derecha
//if the cursor is at the end of the field
if (event.target.textLength-event.target.selectionStart == 0) {
$(adelante).focus();
}
break;
textLength沒有為我捕獲字段文本長度; 我使用了event.target.value.length
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.