javascript 未捕获的 ReferenceError:未定义 myFunction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15171294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: myFunction is not defined
提问by fuddin
Gives an error. I have placed the code just before </body>
. Still getting the error.
给出一个错误。我已经在之前放置了代码</body>
。仍然收到错误。
<form action="" method="get" id="searchform" >
<input name="q" type="text" id="search" size="32" maxlength="128" class="txt">
<input type="button" id="hit" value="Search" onclick="myFunction();return false" class="btn">
</form>
JS,
JS,
<script type="text/javascript">
var nexturl = "";
var lastid = "";
var param;
$(document).ready(function () {
function myFunction() {
param = $('#search').val();
alert("I am an alert box!");
if (param != "") {
$("#status").show();
var u = 'https://graph.facebook.com/search/?callback=&limit=100&q=' + param;
getResults(u);
}
}
$("#more").click(function () {
$("#status").show();
$("#more").hide();
pageTracker._trackPageview('/?q=/more');
var u = nexturl;
getResults(u);
});
});
</script>
回答by ATOzTOA
You cannot place myFunction
after the onclick
. When the onclick
is seen there is no definition for myFunction
.
你不能把myFunction
后onclick
。当onclick
看到 时 没有定义myFunction
。
Place the JavaScript in <head>
tag. Also, move the function outside of ready()
.
将 JavaScript 放入<head>
标签中。此外,将函数移出ready()
.
Like this:
像这样:
<script type="text/javascript">
var nexturl ="";
var lastid ="";
var param;
function myFunction() {
param = $('#search').val();
alert("I am an alert box!");
if (param != "") {
$("#status").show();
var u = 'https://graph.facebook.com/search/?callback=&limit=100&q='+param;
getResults(u);
}
}
$(document).ready(function() {
$("#more").click(function () {
$("#status").show();
$("#more").hide();
pageTracker._trackPageview('/?q=/more');
var u = nexturl;
getResults(u);
});
});
</script>
</head>
<body>
...
回答by PSR
keep myFunction in script tag directly
将 myFunction 直接保存在 script 标签中
i.e
<script>
function myFunction() {
.....
}
</script>
回答by Cianan Sims
From the jQuery docs:
来自jQuery 文档:
The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.
传递给 .ready() 的处理程序保证在 DOM 准备好后执行,因此这通常是附加所有其他事件处理程序和运行其他 jQuery 代码的最佳位置。
So your function isn't created until after your onclick is established. Thus it can't find the function. You'll want to move it outside the $(document).ready(function(){})
.
因此,直到您的 onclick 建立之后,您的函数才会被创建。因此它无法找到该函数。你会想把它移到$(document).ready(function(){})
.
回答by Rodolfo Velasco
You have to move the function outside the $document.ready
here then you will have two options: 1st move it to <head>
or 2nd move it right before the closing $document.ready
bracket. Use this type of declaration for your function myFunction(){alert("inside my function");};
您必须将函数移到$document.ready
此处之外,然后您将有两个选择:第一个将其移动到<head>
或第二个将其移动到右$document.ready
括号之前。为您的函数使用这种类型的声明myFunction(){alert("inside my function");};