문제

I've create a JavaScript function that fires on the OnKeyPress event of a text box. Basically when the function is called a label is displayed telling the user that caps lock is enabled. As you can see from the method below, if the Caps Lock is on 3 labels are displayed but I'd like to use the passed in parameter to find out which textbox fired the event so that I only display one of the caps lock labels. Any ideas

My JScript function is below with one of the calling textboxs below that:

    function capLock(e) {
        kc = e.keyCode ? e.keyCode : e.which;
        sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
        if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk))
            document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility = 'visible';
            document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'visible';
            document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'visible';
        else
            document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility = 'hidden';
            document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'hidden';
            document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'hidden';
    } 

<asp:TextBox ID="txtCurrentPassword" runat="server" TextMode="Password" CssClass="customtxt" onkeypress="capLock(event)"></asp:TextBox>

So I'd like to do something like the following with the JScript Function:

if (e.name=="txtCurrentPassword") 
{
    document.getElementById('divCurrentPasswordCapsLockStatus').style.visibility ='visible';
}
else 
{ 
    document.getElementById('divNewPasswordCapsLockStatus').style.visibility = 'hidden';
    document.getElementById('divConfirmPasswordCapsLockStatus').style.visibility = 'hidden';
}

I'm using ASP.Net 4.0

Thanks

도움이 되었습니까?

해결책

Ok so you need to use

ClientIdMode="Static"

See a breakdown of what the modes are http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx

Pretty much this will emit IDs as you specify them in the page, not as the naming container tries and puts them as.

Then can you pass 'this' into your function? e.g.

onkeypress="capLock(this, event)" 

and change your function to be

function capLock(sender, e) {
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top