简体   繁体   English

控制台错误:[function] 未定义,尽管它是

[英]Console error: [function] is not defined, although it is

I'm calling my function in my HTML, and even though the function originates in the same file I'm receiving an error stating that the function is undefined.我在我的 HTML 中调用我的函数,即使该函数源自同一个文件,我也收到一条错误消息,指出该函数未定义。 I cleared the browser cache, but with no luck.我清除了浏览器缓存,但没有运气。

I tried calling the function in a click event in the same <script> , but the click event wasn't recognized at all:我尝试在同一个<script>的点击事件中调用该函数,但根本无法识别点击事件:

$("#addFileButton").on("click", function() {
  uploadFile();
  console.log("click")
})

This is what my code looks like now:这就是我的代码现在的样子:

<!-- HTML -->
<input id="getFile" type="file" multiple="multiple"/><br />
<input id="addFileButton" type="button" value="upload" onclick="uploadFile();" />
<!-- etc -->
</div>

<script>
  function uploadFile() {
    // code
  }
</script>

JSFiddle link: https://jsfiddle.net/Tsardines/Lrcgfjhq/5/ JSFiddle 链接: https ://jsfiddle.net/Tsardines/Lrcgfjhq/5/

I have validated, It is working fine.我已经验证,它工作正常。 see here - jsbin.com/nuzuqomoku/edit?html,js,console,output看这里 - jsbin.com/nuzuqomoku/edit?html,js,console,output

Add a click event through jquery and then call the uploadFile method inside it通过jquery添加点击事件,然后调用里面的uploadFile方法

 <html> <head> <!-- jquery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <!-- Bootstrap JS --> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous" ></script> </head> <body> <!-- HTML --> <input id="getFile" type="file" multiple="multiple"/><br /> <input id="addFileButton" type="button" value="upload" /> <!-- etc --> </div> <script> $(document).ready(function(){ function uploadFile(){ alert('File Uploaded'); } $("#addFileButton").on("click", function() { uploadFile(); console.log("click") }); }); </script> </body> </html>

If this is the code:如果这是代码:

<!-- HTML -->
<input id="getFile" type="file" multiple="multiple"/><br />
<input id="addFileButton" type="button" value="upload" onclick="uploadFile();" />
<!-- etc -->
</div>

<script>
  function uploadFile() {
    // code
  }
</script>

I don't see a problem with this code.我看不出这段代码有问题。 Please make sure that you save and load the right file.请确保您保存和加载正确的文件。 You might just forget to load the right file.您可能只是忘记加载正确的文件。 ^_^ ^_^

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

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