2015-06-24 117 views
0

顯示器:jQuery的onkeyup事件:掃描未定義

Uncatched Reference error: scan not defined.

下面是代碼:

<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> 
+0

不要把util函數放在一個就緒狀態,它們需要是全局的才能被內聯js看到。因爲他們自己什麼都不做,所以沒有必要推遲。 – dandavis

+0

什麼是掃描功能範圍? – MaxZoom

回答

0

你的掃描功能僅在範圍訪問:

function() { 

    function scan() { ... } 

} 

你應該把它放在這個功能之外,在文檔的級別:

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

您應該使用$(document).ready來初始化文檔,一旦它的內容被加載。在這裏,你只是在我剛剛提到的初始化函數的範圍內聲明瞭掃描函數,但它不在onkeyup事件中設置的事件處理函數的範圍內。因此,找不到功能掃描。

如果您還有其他問題,請告訴我。如果解決了這個問題,別忘了投票! :)

+0

非常感謝!我非常感謝你的幫助。你的解決方案是對的。 :) –

+0

非常歡迎您!很高興我能幫上忙! –

1

scan方法定義應該是其他函數的範圍之外。看你如何修改代碼:

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

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

非常感謝! –

+0

順便說一句,你會說俄語嗎? –

+0

@VladimirChampion yup – Andrei