3

我有一个网站形式的脚本,其中按回车键或向右箭头将焦点更改为下一个字段,或者按向左箭头将焦点更改为上一个字段。

我需要能够在编辑文本时使用箭头键更改光标位置,而无需更改字段焦点,并且仅当光标位于文本的开头或结尾时才转到下一个字段。

我怎么能做到这一点?

脚本代码如下:

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" >
4

1 回答 1

1

例如:

        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;
于 2012-09-18T00:37:54.927 回答