English 中文(简体)
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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签