Javascript 函数未定义 - 未捕获的引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5067887/
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
Function is not defined - uncaught referenceerror
提问by Peter
I have this uncaught referenceerror function is not defined error which do not understand.
我有这个未捕获的referenceerror 函数未定义错误,不明白。
If I have
如果我有
$(document).ready(function(){
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
});
and
和
<input type="image" src="btn.png" alt="" onclick="codeAddress()" />
<input type="text" name="formatedAddress" id="formatedAddress" value="" />
When I press the button it will return the "uncaught referenceerror".
当我按下按钮时,它将返回“未捕获的引用错误”。
But if I put the codeAddress() outsidethe $(document).ready(function(){} then it working fine.
但是,如果我将 codeAddress()放在$(document).ready(function(){} 之外,那么它工作正常。
My intention is put the codeAddress() within the document.ready.function.
我的意图是将 codeAddress() 放在 document.ready.function 中。
回答by Humberto
The problem is that codeAddress()
doesn't have enough scope to be callable from the button. You must declare it outside the callback to ready()
:
问题是codeAddress()
没有足够的范围可以从按钮调用。您必须在回调之外将其声明为ready()
:
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
$(document).ready(function(){
// Do stuff here, including _calling_ codeAddress(), but not _defining_ it!
});
回答by ifaour
How about removing the onclick
attribute and adding an ID:
如何删除onclick
属性并添加一个 ID:
<input type="image" src="btn.png" alt="" id="img-clck" />
And your script:
还有你的脚本:
$(document).ready(function(){
function codeAddress() {
var address = document.getElementById("formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
}
$("#img-clck").click(codeAddress);
});
This way if you need to change the function name or whatever no need to touch the html.
这样,如果您需要更改函数名称或不需要触摸 html 的任何内容。
回答by Jonathon Bolster
Your issue here is that you're not understanding the scope that you're setting.
您的问题是您不了解您设置的范围。
You are passing the ready
function a function itself. Within this function, you're creating another function called codeAddress
. This one exists within the scope that created it and not within the window object (where everything and its uncle could call it).
您正在将ready
函数传递给函数本身。在此函数中,您正在创建另一个名为 的函数codeAddress
。这个存在于创建它的范围内,而不是在 window 对象中(所有东西和它的叔叔都可以调用它)。
For example:
例如:
var myfunction = function(){
var myVar = 12345;
};
console.log(myVar); // 'undefined' - since it is within
// the scope of the function only.
Have a look here for a bit more on anonymous functions: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
在这里查看更多关于匿名函数的信息:http: //www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
Another thing is that I notice you're using jQuery on that page. This makes setting click handlers much easier and you don't need to go into the hassle of setting the 'onclick' attribute in the HTML. You also don't need to make the codeAddress
method available to all:
另一件事是我注意到您在该页面上使用了 jQuery。这使得设置点击处理程序变得更加容易,并且您无需在 HTML 中设置“onclick”属性的麻烦。您也不需要让codeAddress
所有人都可以使用该方法:
$(function(){
$("#imgid").click(function(){
var address = $("#formatedAddress").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
});
});
(You should remove the existing onclick
and add an ID to the image element that you want to handle)
(您应该删除现有的onclick
并为要处理的图像元素添加一个 ID)
Note that I've replaced $(document).ready()
with its shortcut of just $()
(http://api.jquery.com/ready/). Then the clickmethod is used to assign a click handler to the element. I've also replaced your document.getElementById
with the jQuery object.
请注意,我已替换$(document).ready()
为它的快捷方式$()
(http://api.jquery.com/ready/)。然后使用click方法为元素分配一个点击处理程序。我也用document.getElementById
jQuery 对象替换了你。
回答by Taimoor Changaiz
You must write that function body outside the ready();
您必须在 ready() 之外编写该函数体;
because ready is used to call a function or to bind a function with binding id like this.
因为 ready 用于调用一个函数或绑定一个具有绑定 id 的函数,就像这样。
$(document).ready(function() {
$("Some id/class name").click(function(){
alert("i'm in");
});
});
but you can't do this if you are calling "showAmount" function onchange/onclick event
但是如果你在 onchange/onclick 事件中调用“showAmount”函数,你就不能这样做
$(document).ready(function() {
function showAmount(value){
alert(value);
}
});
You have to write "showAmount" outside the ready();
你必须在 ready() 之外写“showAmount”;
function showAmount(value){
alert(value);//will be called on event it is bind with
}
$(document).ready(function() {
alert("will display on page load")
});
回答by nayanajith
Please check whether you have provided js references correctly. JQuery files first and then your custom files. Since you are using '$' in your js methods.
请检查您是否正确提供了 js 引用。首先是 JQuery 文件,然后是您的自定义文件。由于您在 js 方法中使用了 '$'。