简体   繁体   中英

What is the best way to jump from one textbox to another using the Microsoft client-side AJAX Library?

I'm trying to find a reusable way to set focus from one text box to another upon enter using ASP.NET, but using client-side JavaScript to do so.

The only reason I mention this is to be done in ASP.NET, is due to the fact that client Id's of the controls that ASP.NET renders can be different than what was specified in the markup.

Modified following code to get your goal.

    /***********************************************
    * Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
    * This notice must stay intact for use
    * Visit http://www.dynamicdrive.com/ for full source code
    ***********************************************/

function handleEnter(field, event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < field.form.elements.length; i++)
                if (field == field.form.elements[i])
                break;
            i = (i + 1) % field.form.elements.length;
            field.form.elements[i].focus();
            return false;
        }
        else
            return true;
    }

You could try this.

function ChangeOnEnter (event, target) {
     if(event.keyCode === 13){
         document.getElementById(target).focus();
         return false;
     }
}

<input type="text" id="first" onKeyPress="ChangeOnEnter(event,'second')"/>
<input type="text" id="second"/>

Hope it's what you're looking for.

You could make use of the tabindex html element property to allow users to through the fields. This is, in my opinion, a universal mechanism for moving through a form.

http://www.w3.org/TR/html401/interact/forms.html#adef-tabindex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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