2013-03-21 84 views
7

我在使用ASP.net C#開發的登錄頁面出現問題。 該表單適用於IE,但不適用於Firefox。 這裏是我的示例:event.keyCode在Firefox中不工作

<%@ Page Language="c#" Inherits="ClubCard.loginClubcard" CodeFile="loginClubcard.aspx.cs" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
<html> 
    <head> 
     <title> 
      Acceso Clubcard 
     </title> 

     <meta http-equiv="Page-Enter" content="blendTrans(Duration=0.25)" /> 
     <meta http-equiv="Page-Exit" content="blendTrans(Duration=0.25)" /> 
     <link rel="stylesheet" type="text/css" href="styles.css"> 

     <style> 
      #centered 
      { 
       LEFT: 50%; 
       MARGIN-LEFT: -235px; 
       POSITION: absolute; 
       TOP: 150px 
      } 

      .letras 
      { 
       FONT-SIZE: 10pt; 
       COLOR: white; 
       FONT-FAMILY: Arial 
      } 

      .cajas 
      { 
       FONT-SIZE: 10pt; 
       FONT-FAMILY: Arial 
      } 

      .letrasGris A 
      { 
       FONT-SIZE: 12px; 
       COLOR: #8f8f92; 
       FONT-FAMILY: Arial; 
       TEXT-DECORATION: none 
      } 

      .highlightit IMG 
      { 
       FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=100); 
       -moz-opacity: 1 
      } 

      .highlightit:hover IMG 
      { 
       FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=50); 
       -moz-opacity: 0.5 
      } 
     </style> 

     <script language="javascript">    
      function actLogin() 
      { 
       __doPostBack('btnLogin', ''); 
      } 

      function actFocus() 
      { 
       tbxPrincipal = document.getElementById("tbxUsername").focus(); 
      } 

      function ValidarEnter(src) 
      { 
       if (event.keyCode == "13") 
       { 
        actLogin(); 
       } 
      } 

      function AbrirLogin() 
      { 
       if (window.frameElement != null) 
       { 
        window.parent.location.replace(window.location.href); 
       } 
      } 
     </script> 

     <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> 
     <meta name="CODE_LANGUAGE" content="C#"> 
     <meta name="vs_defaultClientScript" content="JavaScript"> 
     <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> 
    </head> 

    <body bgcolor="#ffffff" leftmargin="0" topmargin="0" onload="AbrirLogin();actFocus()"> 
     <form id="Form1" method="post" runat="server" target="_top"> 
      <table id="Table_01" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> 
       <tr> 
        <td style="background: url(images/login/loginBg.gif) repeat-x"> 
         &nbsp; 
        </td> 
       </tr> 
      </table> 

      <div id="centered"> 
       <div style="left: 50%; margin-left: -100px; position: absolute"> 
        <div> 
         <img src="images/login/logoClubCard.gif"> 
        </div> 
       </div> 

       <div style="margin-left: 100px; width: 200px; position: absolute; top: 240px"> 
        <table width="100%" cellpadding="0" cellspacing="2" border="0"> 
         <tr> 
          <td align="right" class="letras"> 
           USUARIO: 
          </td> 

          <td> 
           <asp:TextBox runat="server" CssClass="cajas" ID="tbxUsername"> 
           </asp:TextBox> 
          </td> 

          <td rowspan="3"> 
           &nbsp;&nbsp; 
           <a class="highlightit" href="javascript:actLogin()" tabindex="3"> 
            <img border="0" src="images/login/botonLogin.gif"> 
           </a> 
          </td> 
         </tr> 

         <tr> 
          <td align="right" class="letras"> 
           CLAVE: 
          </td> 

          <td> 
           <asp:TextBox runat="server" CssClass="cajas" ID="tbxPassword" TextMode="Password" OnKeyPress="ValidarEnter(this)" OnTextChanged="tbxPassword_TextChanged"> 
           </asp:TextBox> 
          </td> 
         </tr> 
        </table> 
       </div> 

       <div style="margin-top: 54px"> 
        <img src="images/login/cajaAzul.gif"> 
       </div> 

       <div style="display: inline; float: left;width:50% " align="center"> 
        <asp:RequiredFieldValidator ID="rfvUsername" runat="server" 
         CssClass="tA" ErrorMessage="* Escriba su USUARIO" 
         ControlToValidate="tbxUsername"> 
        </asp:RequiredFieldValidator> 

        <br> 

        <asp:RequiredFieldValidator ID="rfvPassword" runat="server" 
         CssClass="tA" ErrorMessage="* Escriba su CLAVE" 
         ControlToValidate="tbxPassword"> 
        </asp:RequiredFieldValidator> 

        <br> 

        <table > 
         <tr align="center"> 
          <td> 
           <asp:Label runat="server" ID="lblError" Visible="False"> 
            <font color="red" class="tA"> 
             <b> 
              El USUARIO y CLAVE no coincidieron, trate de nuevo 
             </b> 
            </font> 
           </asp:Label> 
          </td> 
         </tr> 
        </table> 
       </div> 
      </div> 

      <div style="display: none"> 
       <input type="button" runat="server" id="btnLogin" value="login" onserverclick="btnLogin_ServerClick"> 
      </div> 
     </form> 
    </body> 
</html> 

我注意到,在這部分...

function ValidarEnter(src) 
      { 
       if (event.keyCode == "13") 
       { 
        actLogin(); 
       } 
      } 

當我調試它在Firefox,它只是跳過它。 我已經嘗試添加window.event.keyCode ...並且僅用13切換「13」... 我該如何解決這個問題?

回答

9

event.keycode不被Firefox支持。使用event.which for firefox。

function checkKey(evt) { 
    var keyID = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode); 
    alert(keyID); 
} 
+0

對不起,它不工作了...它實際上停止了對IE的工作=( – Victor 2013-03-21 19:09:01

+0

請使用我的更新答案 – Amit 2013-03-21 19:13:44

+0

仍然沒有成功......我欣賞的幫助......在工作IE雖然 – Victor 2013-03-21 19:30:23

0

功能key_fir(E) { E = ||ë window.event || {}; var charCode = e.charCode || e.keyCode || e.which; 如果(則charCode == 13){

   //write your code here Like $("#log_sub").click(); 
      } 
} 
2

我工作的一個項目,當我面臨着同樣的問題。我做了一些RND並找出這兩件事;

  1. e.keyCode正常工作與onkeydown()功能,無論是瀏覽器Chrome的火狐

下面是兩個 & 火狐良好做法

// HTML (Note onkeydown() function here) 
<input id="anyId" onkeydown="return myFunction(event)" type="text" /> 

// JavaScript for it 
function myFunction(e) { 
    var asciiValue = e.keyCode ; 
    alert ("ASCII Value: " + asciiValue); // ASCII of pressed key 
    if(asciiValue >0){ 
     return true; 
    } 
    else{ 
     return false; 
    } 
} 
  • 但是,如果我們在使用onkeypress()功能的Firefox,然後e.keyCode將無法​​正常工作。爲此,採用var asciiValue= e.keyCode || e.charCode
  • // HTML (Note onkeypress() function here) 
    <input id="anyId" onkeypress="return myFunction(event)" type="text" /> 
    
    // JavaScript for it 
    function myFunction(e) { 
        var asciiValue = e.keyCode || e.charCode ; // It'll handle both Chrome & Firefox 
        alert ("Ascii Value: " + asciiValue); // ASCII of pressed key 
        if(asciiValue>0){ 
         return true; 
        } 
        else{ 
         return false; 
        } 
    }