简体   繁体   中英

how to pass javascript parameter from asp.net to .focus

I´ma bit confused here. The thing is that I have an asp.net textbox that I want to tab to the next textbox when maxlength is true and the user does not hit certain keys. My asp.net code is like this:

<asp:TextBox ID="txbTransferBanknumber" Style="width: 50px;" MaxLength="4" 
Visible="true" runat="server" autocomplete="off"    onkeyup="autoTabNextTextBox(this,'txbTransferLedger')" CssClass="AutoTabFill" />

my autoTabNextTextBox function looks like this:

function autoTabNextTextBox(sender, nextTextBox) {

    var keyCode = (event.which) ? event.which : event.keyCode
    var mylength = $(sender).val().length;
    var maxlength = $(sender).attr('maxLength');

    if((keyCode != 37) && (keyCode != 39))
    {
        if ((keyCode != 8) && (keyCode != 16))
        {
            if (mylength == maxlength)
            {   
                $('.nextTextBox').focus();
                //nextTextBox.focus();                    
            }
        }
    }

}

How do I pass the parameter nextTextBox to .focus() ?

Assuming that you have a ClientIdMode of Static (meaning the id of the output HTML element is txbTransferLedger ) then you just need to include the nextTextBox parameter as part of the selector (as opposed to the string nextTextBox which you are currently using):

$('#' + nextTextBox).focus();

Update: If you cannot use a ClientIdMode of Static and therefore don't have full control over the generated ids of the input elements then another option is to use a class selector instead. Give each input element a unique class name (this can be in addition to any existing classes you are using) eg:

<asp:TextBox ID="txbTransferLedger" CssClass="AutoFill TransferLedger" />

Then pass the the class name you want ( TransferLedger in this example) into the autoTabNextTextBox function instead of the ID. You can then use a class selector as follows:

$('input.' + nextTextBox).focus(); // e.g. $('input.TransferLedger').focus();

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