2014-06-15 4 views
0

사용자 이름을 삽입 할 텍스트 필드가있는 양식이 있습니다. JavaScript를 사용하여 사용자가 이름을 입력 한 다음 PHP 파일과 결합하여 사용자 이름이 이미 데이터베이스에 있는지 확인합니다. 페이지가 예상대로 작동하며 사용자가 이름을 입력 할 때마다 이름이 사용 가능한지 JavaScript가 표시됩니다.페이지를 다시로드 한 후에 만 ​​JavaScript가 실행됩니다.

내 문제는 이제 JavaScript가 브라우저에서 페이지를 다시로드 한 후에 만 ​​작동한다는 것입니다. 처음 페이지가로드 될 때 사용자가 이름을 입력해도 아무런 반응이 없습니다. 페이지를 다시로드하면 작동합니다.

어떤 도움을 환영합니다. 어떤 이벤트가 함수에 연결 유지하지 않았기 때문에 이런 일이 발생

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>RestAppXXI</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="shortcut icon" href="assets/ico/favicon.ico"> 
<link href="jquery-mobile/jquery.mobile.theme-1.0.min.css" rel="stylesheet" type="text/css"/> 
<link href="jquery-mobile/jquery.mobile.structure-1.0.min.css" rel="stylesheet" type="text/css"/> 
<script type="text/javascript"> 
//function to check username availability 
function check_availability(){ 

     //get the username 
     var username = $('#username').val(); 

     //use ajax to run the check 
     $.post("check_username.php", { username: username }, 
      function(result){ 
       console.log(result); 
       //if the result is 1 
       if(result == 1){ 

        //show that the username is available 
        $('#username_availability_result').html(username + ' está disponible'); 
       }else{ 

        //show that the username is NOT available 
        $('#username_availability_result').html(username + ' NO está disponible'); 
       } 
     }); 

} 
</script> 

<script type="text/javascript" src="js/jquery.js"></script> 
<script src="jquery-mobile/jquery-1.6.4.min.js" type="text/javascript"></script> 
<script src="jquery-mobile/jquery.mobile-1.0.min.js" type="text/javascript"></script> 
</head> 
<body> 
<div data-role="page" id="page" data-theme="a"> 
    <div data-role="header"> 


     <h1>Nuevo Personal</h1> 
    </div> 
    <div data-role="content"> 
     <div align="center"> 
     <p><a href="plogin-sp.html" data-ajax="false"></a><img src="logorestappxxi.jpg" width="75" height="75"></p> 



     <p>[email protected] <?php echo 
     $_SESSION[myusername]."/".$_SESSION[Region]?></p> 

     </div> 
     <section id="c5"> 
    <div class="page-header"> 
      <h1 align="center">Nuevo Personal<?php echo "<span class='label label-important'> $count</span>"; ?></h1> 
     </div> 
    <div align="center"> 

     <?php 
if (empty($_POST)){ 
?> 
    </div> 
    <form name='registration' data-ajax="false" action='nuevomesero.php' method='POST' /> 

     <div align="center">Alias: </div> 

    <div align="center"> 
     <p> 
     <input type="text" name="aliasq" id='username' onkeyup="check_availability()"/><div id='username_availability_result'></div> 
     </p> 
     <p>&nbsp; </p> 
     <div align="center"> 
     <div align="center">Nombre completo: </div> 

    <input type="text" name="nombreq" /> 
    </div> 

    <div align="center"> 
    <div align="center">Restaurante: </div> 
    <td><p> 
     <select name="restaurante"> 
     <?php 
do { 
?> 
     <option value="<?php echo $row_Recordset1['id_restaurante']?>" ><?php echo $row_Recordset1['nombre_restaurante']?></option> 
     <?php 
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); 
?> 
     </select> 
    </p></td> 
    <div align="center">Contraseña: </div> 

    <input type="text" name="passq" /> 
    <button type="submit" >Añadir el nuevo personal y regresar</button> 
     </form> 
    <?php 
} else { 
} 


?> 
    </div> 
    </div> 
     </section> 
    </div> 

</div> 



</body> 
</html> 

답변

0

: 다음은 내 코드입니다. 는 형태

<script type="text/javascript"> 
$(document).ready(function(){ 
//function to check username availability 
function check_availability(){ 

    $('#username').keyup(function(){ 
     //get the username 
     var username = $('#username').val(); 

     //use ajax to run the check 
     $.post("check_username.php", { username: username }, 
      function(result){ 
       console.log(result); 
       //if the result is 1 
       if(result == 1){ 

        //show that the username is available 
        $('#username_availability_result').html(username + ' está disponible'); 
       }else{ 

        //show that the username is NOT available 
        $('#username_availability_result').html(username + ' NO está disponible'); 
       } 
     }); 
    }); 
}); 

} 
</script> 
+0

이 – Wanderer

+0

감사합니다 코멘트 친구로이 추가 이름을 입력 한 후 immedaitely 제출 이전에 함수를 호출 하시겠습니까. 이름을 입력 한 직후 함수를 호출하고 싶습니다. 이것은 함수 호출입니다 : onkeyup = "check_availability()" – rzimmerman

+0

내 담당자가 여전히 적은 이유는 무엇입니까 – Don

관련 문제