2010-01-27 143 views
1

考慮:JavaScript函數沒有被定義

文件的script.js

function AdaugaComboBox(id, name){ 
     var select_tag = document.getElementById(id); 
     select_tag.innerHTML += "<select name='"+name+"'><option value='0'>Selecteaza autor</option></select><br/>"; 
     return true; 
    } 

和文件index.html的

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

    <body> 
     <table> 
     <tr> 
      <td id="autori">...</td> 
     </tr> 
     <tr> 
      <td> 
       <input type="button" 
         value="Adauga autor" 
         onclick="AdaugaComboBox('autori', 'autori[]')"/> 
      </td> 
     </tr> 
     </table> 
    </body> 
</html> 

功能的範圍是將一個組合框添加到TABLE中的特定TD。但是,當我按下按鈕會出現這樣的錯誤:

AdaugaComboBox沒有定義

爲什麼?


更新:

!我修復了它。問題出在另一個功能上。

+2

script.js不包含在HTML中的任何位置...... – Langdon 2010-01-27 12:32:18

+0

在文檔樹中的哪個位置定義了函數? – 2010-01-27 12:32:49

+0

script.js包含在文檔中,我得到相同的錯誤。 – Emanuel 2010-01-27 12:40:21

回答

4

如果腳本包含在HTML,那麼它可能是你沒有正確的基於HTML文件的位置的路徑。檢查Firefox/Firebug以確保JS文件正確下載。

1

您必須對script.js文件進行引用。

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

+0

script.js包含在文檔中,並且出現相同的錯誤。 – Emanuel 2010-01-27 12:47:17

1

你的HTML應該是:

<html> 
<head> 
    <script src="script.js" type="text/javascript"></script> 
</head> 
<body> 
<table> 
<tr> 
    <td id="autori">...</td> 
</tr> 
<tr> 
    <td> 
    <input type="button" value="Adauga autor" onclick="AdaugaComboBox('autori', 'autori[]')"/> 
    </td> 
</tr> 
</table> 
</body> 
</html> 
+0

script.js包含在文檔中,我得到相同的錯誤。 – Emanuel 2010-01-27 12:46:33

+0

然後,您的script.js文件不能位於名爲「js」的子目錄中,因爲代碼正常工作。 – Langdon 2010-01-27 12:51:13