2014-10-13 7 views
1

나는 지금 내 스크립트 내가 로그인 기능의 키는

$(function() { 

    $(".enter_popup").keypress(function (e) { 
     if (e.keyCode == 13) { 
      if ($(this).attr('id') == "txtUserName" || $(this).attr('id') == "txtPassword") { 
       LogIn(); 
      } 

     } 
    }); 
}); 

function LogIn() { 
    var username = $('#txtUserName').val(); 
    var password = $('#txtPassword').val(); 
} 

를 호출하고있어이 텍스트 상자와 버튼

<table cellpadding="0" cellspacing="0"> 
     <tr> 
      <td align="center"><input name="" type="text" value="" title="User Name" class="width190 enter_popup" id="txtUserName" onfocus="txtFocus(this)" onblur="txtFBlur(this,'0')"/></td> 
      <td align="center"><input name="" type="password" value="" title="Password" class="width190 enter_popup" id="txtPassword" onfocus="txtFocus(this)" onblur="txtFBlur(this,'1')"/></td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"><input name="" type="submit" class="blue_btn" value="Sign In" id="btnLogIn" onclick="javascript:return LogIn()"/></td> 
     </tr> 

     </table> 

을 가지고 있지만 키를 누를 때 해고되지 않은 JQuery와 모달 대화 상자를 열 .. 왜 어떤 아이디어가 있고 그 해결책은 무엇입니까?

+1

여기 잘 작동 http://jsfiddle.net/dzxu1c8s/그래서 요소가 동적으로 생성되어'event-delegation'이 필요하다고 생각합니다. $ (document) .on ('keypress', 'enter_popup', function() {/ * code * /}); ' – Anton

+0

또한 코드가 작동하는지 확인했습니다. 위의 Anton의 해결책을 시도하십시오. – mikelt21

+1

@Anton :이'$ (document) .on ('keypress', 'enter_popup', function() {/ * code * /});'나를 위해 일했다. –

답변

3

당신은 동적으로 생성 된 요소에 이벤트 위임을 사용할 필요가

$(document).on('keypress','.enter_popup',function(){ 
    /*Your code*/ 
}); 
0

대신에서 KeyDown 또는의 keyup 이벤트를 사용하려고 :

$(function() { 

$(".enter_popup").keyup(function (e) { 
    if (e.keyCode == 13) { 
     if ($(this).attr('id') == "txtUserName" || $(this).attr('id') == "txtPassword") { 
      LogIn(); 
     } 

    } 
});