如何修复ReferenceError:未定义X

时间:2019-01-08 18:49:03

标签: html ajax

我收到一个错误,表明未定义我的函数“ loaddata”。但是,我没有将脚本放置在页面顶部。任何想法为什么会这样?

谢谢!

<script type="text/javascript">
  function loaddata(){
     var sid = document.getElementById("student_id");
     if(sid) {
        $.ajax({
            type: 'post',
            url: 'search.php',
            data: {
              student_id:sid,
            },
            success: function (response) {
           // We get the element having id of display_info and put the response inside it
             $( '#display_info' ).html(response);
            }
         });
      }
      else
      {
        $( '#display_info' ).html("Please Enter Some Words");
      }
   }
</script>    

</head>
<body>     
   <div class="content">
   <H2>Lookup Member</H2> 
   <input type="text" name="student_id" id="student_id" onkeyup="loaddata();">
   </div>    
</body>

1 个答案:

答案 0 :(得分:0)

在一个HTML文件中为我工作。 我评论您的jquery代码只是为了测试。

<head>
    <script type="text/javascript">
        function loaddata() {
            var sid = document.getElementById("student_id");

            if (sid) {
                // $.ajax({
                //     type: 'post',
                //     url: 'search.php',
                //     data: {
                //         student_id: sid,
                //     },
                //     success: function(response) {
                //         // We get the element having id of display_info and put the response inside it
                //         $('#display_info').html(response);
                //     }
                // });
            } else {
                $('#display_info').html("Please Enter Some Words");
            }
        }
    </script>    
</head>

<body>
    <div class="content">
        <H2>Lookup Member</H2>
        <input type="text" name="student_id" id="student_id" onkeyup="loaddata();">
    </div>  
</body>