2010-01-27 89 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