简体   繁体   English

jQuery onkeyup事件:扫描未定义

[英]jQuery onkeyup event: scan not defined

Displays: 显示:

Uncatched Reference error: scan not defined. 未捕获的参考错误:未定义扫描。

Here is the code: 这是代码:

<html>
<head>
    <script type="text/javascript" src="../js/jquery.js"></script> <script type="text/javascript">

        $(document).ready(function() {
            function scan() {
                $("#result").text("<font style='color: green;'>Cote</font>");   console.log("Function ran");
    }

});

    </script>
</head>
<body>
    <font style="font-size: 84px;">Scan card, please</font>
        <p id="result">cotes</p>
            <input style="font-size: 36px;" id="input" maxlength=16 autofocus onkeyup="scan();">
</body> 
</html>

scan method definition should be outside of the other function's scope. scan方法定义应在其他函数的范围之外。 See how you should modify your code: 查看如何修改代码:

function scan() {
    $("#result").text("<font style='color: green;'>Cote</font>");
    console.log("Function ran");
}

$(document).ready(function() { 
    // remove if not necessary
});

Your scan function is only accessible in the scope of: 您的扫描功能只能在以下范围内使用:

function() {

   function scan() { ... }

}

You should place it outside this function at the level of the document: 您应该将其放在文档级别的此功能之外:

<script type='text/javascript'>
     function scan() { ... }
</script>

You should use $(document).ready for initializing the document once its content has been loaded. 加载文档的内容后,应使用$(document).ready对其进行初始化。 Over here, you are just declaring the scan function within the scope of this initialization function I've just mentioned but it is not within the scope of the event handler set at the onkeyup event. 在这里,您只是在刚才提到的此初始化函数的范围内声明了扫描函数,但它不在onkeyup事件中设置的事件处理程序的范围之内。 Therefore, function scan is not found. 因此,找不到功能扫描。

If you have any further questions, let me know. 如果您还有其他问题,请告诉我。 If this solves the issue, don't forget to vote up! 如果这样可以解决问题,请别忘了投票! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM