简体   繁体   English

导航表单字段并使用箭头键编辑文本

[英]navigate form fields and edit text with the arrow keys

I have a script in a form of a website, where pressing the enter key or the right arrow changes focus to the next field, or by pressing the Left arrow changes focus to the previous field. 我有一个网站形式的脚本,按下回车键或右箭头将焦点更改为下一个字段,或者按向左箭头将焦点更改为前一个字段。

I need to be able to use the arrow keys to change the cursor position while editing the text, without change the field focus and only go to the next field when the cursor is at the beginning or end of the text. 我需要能够在编辑文本时使用箭头键更改光标位置,而不更改字段焦点,只有当光标位于文本的开头或结尾时才转到下一个字段。

how i can make this? 我怎么能做到这一点?

The script code is as follows: 脚本代码如下:

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;
}

the html code is: HTML代码是:

<s:textfield name="programaFaena.fecha"  maxlength="10" tabindex="1" onkeypress="return tabulacion_manual(event,this,next())" autocomplete="off" readonly="false" >

For example: 例如:

        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 did not capture the field text length for me; textLength没有为我捕获字段文本长度; I used event.target.value.length 我使用了event.target.value.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM