函数未定义 - 未捕获的referenceerror

时间:2011-02-21 15:37:26

标签: javascript

我有这个未被捕获的referenceerror函数未定义错误哪些不明白。

如果我有

$(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);
   }
  });
 }
});

<input type="image" src="btn.png" alt="" onclick="codeAddress()" />
<input type="text" name="formatedAddress" id="formatedAddress" value="" />

当我按下按钮时,它将返回“未被捕获的参考错误”。

但是,如果我将codeAddress()放在之外的$(document).ready(function(){},那么它可以正常工作

我的意图是将codeAddress()放在document.ready.function。

8 个答案:

答案 0 :(得分:45)

问题是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!
});

答案 1 :(得分:37)

如何删除onclick属性并添加ID:

<input type="image" src="btn.png" alt="" id="img-clck" />

你的剧本:

$(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);
});

这种方式如果您需要更改功能名称或任何不需要触摸html。

答案 2 :(得分:22)

您的问题是,您不了解自己设置的范围。

您正在将ready函数传递给函数本身。在此函数中,您将创建另一个名为codeAddress的函数。这个存在于创建它的范围内,而不是在window对象中(所有东西和它的叔叔都可以调用它)。

例如:

var myfunction = function(){
    var myVar = 12345;
};

console.log(myVar); // 'undefined' - since it is within 
                    // the scope of the function only.

在这里查看有关匿名函数的更多信息:http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

另一件事是我注意到你在该页面上使用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);
           }
        });
    });  
});

(您应该删除现有的onclick并将ID添加到您要处理的图像元素中

请注意,我已使用$(document).ready()http://api.jquery.com/ready/)的快捷方式替换了$()。然后click方法用于为元素分配单击处理程序。我还用jQuery对象替换了你的document.getElementById

答案 3 :(得分:14)

您必须在ready();

之外编写该函数体

因为ready用于调用函数或绑定具有绑定id的函数,如此。

$(document).ready(function() {
    $("Some id/class name").click(function(){
        alert("i'm in");
    });
});

但如果您在onchange / onclick事件中调用“showAmount”函数,则无法执行此操作

$(document).ready(function() {
    function showAmount(value){
        alert(value);
    }

});

你必须在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")
});

答案 4 :(得分:1)

还有一件事。以我的经验,发生此错误是因为Function is not defined - uncaught referenceerror之前还有另一个错误。

因此,请在控制台中查看是否存在先前的错误,如果存在,请更正存在的任何错误。您可能很幸运,因为它们是问题所在。

答案 5 :(得分:1)

清除缓存为我解决了这个问题,或者您可以在其他浏览器中打开它

index.js

function myFun() {
    $('h2').html("H999999");
}

index.jsp

<html>
<head>
    <title>Reader</title>
</head>
<body>
<h2>${message}</h2>
<button id="hi" onclick="myFun();" type="submit">Hi</button>
</body>
</html>

答案 6 :(得分:0)

请检查您是否正确提供了js参考。首先是JQuery文件,然后是自定义文件。因为你在js方法中使用'$'。

答案 7 :(得分:0)

我想提一提这是因为我花了一些时间解决了这个问题,而且在SO的任何地方都找不到答案。我正在使用的代码为同事工作,但对我却不行(我遇到了同样的错误)。它在Chrome中对我有效,但在Edge中不起作用。

我能够通过清除Edge中的缓存来使其工作。

这可能不是这个特定问题的答案,但我想我会提到它,以防它节省了别人的时间。

相关问题