gpt4 book ai didi

javascript - html javascript - onclick() 引用错误 : function not defined

转载 作者:行者123 更新时间:2023-11-27 23:22:57 24 4
gpt4 key购买 nike

给定我的 html 文件,控制台给出错误代码:

Reference Error: loadData not defined.

根据其他答案,我重新检查了我的位置,

  1. 我的函数是在 </body> 之前声明的,
  2. 我在 <head></head> 包含了 javascript 库;
  3. 甚至改变了我的 <input type= submit<input type= button
  4. 如果有帮助,我尝试删除包含它的表单标签 (但这是关于查询字符串的另一个问题)。

这是现在的样子:

<head>
<script src = "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
{%for document in documents %}
<li> {{document.filename}}
<input type="button" id="{{ document.id }}" onclick="loadData(this.id)" name = "load-data" value="Add to Layer"/>
</li>
{%endfor%}

<script type ="text/javascript">
function loadData(documentId){
$.ajax({
url:"/load",
data: {'documentId': documentId},
type: 'GET',
success: function(){
window.location.href = "http://127.0.0.1:8000/url/locations";
}
});
}
</script>
</body>

我似乎无法找到为什么函数不会触发的解释。

最佳答案

这是解决方法(正如我的 friend 所解释的):

我不得不将我的脚本放在 <head> 中标签。

为什么? 因为在脚本开始之前文档已经加载。

如果我想将它保留在底部,它必须看起来像这样:

$(document).ready(
$('input').onclick(function(){})

将其放置后,代码就可以工作了。

关于javascript - html javascript - onclick() 引用错误 : function not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40120710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com