3

Webサイトの形式のスクリプトがあり、Enterキーまたは右矢印を押すとフォーカスが次のフィールドに変わり、左矢印を押すとフォーカスが前のフィールドに変わります。

フィールドフォーカスを変更せずに、テキストの編集中に矢印キーを使用してカーソル位置を変更できるようにする必要があります。カーソルがテキストの最初または最後にある場合にのみ、次のフィールドに移動します。

どうすればこれを作ることができますか?

スクリプトコードは次のとおりです。

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 に答える