【问题标题】:Uncaught ReferenceError: [function] is not defined未捕获的 ReferenceError:[function] 未定义
【发布时间】:2023-03-20 11:56:01
【问题描述】:

我正在尝试在我的页面中实现 Googlemaps,但我遇到了一些问题,因为我还是 webdev 的新手。

我遵循了关于将数据保存到数据库的 google API 教程,并在我的 .js 文件中创建了相应的函数。

代码如下所示:

    function createMarker(location){

    if(poiMode && activeMarkerIcon != null) {
        var marker = new google.maps.Marker({
        position: location,
        icon: activeMarkerIcon,
        clickable:true,
        draggable:true,
        animation:null,
        raiseOnDrag:false,
        title: Titel,
        map: map
        });
        useFilter();
    activeGroup.push(marker);  // Marker wird zum dazugehörigenArray hinzugefuegt
    map.setOptions({draggableCursor:'default'}) // Mauscursor wieder in ursprünglichen Zustand versetzen
    $('body, #poi1, #poi2, #poi3, #poi4, #poi5, #poi6, #poi7, #loeschen').removeClass('poi1A poi2A poi3A poi4A poi5A poi6A poi7A deleteA');
      var html = "<table>" +
      "<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
      "<tr><td>Beschreibung:</td> <td><TEXTAREA id='description' rows='10' cols='50'></TEXTAREA></td> </tr>" +
      "<tr align='right'><td></td><td><input type='button' value='Speichern' onclick='saveData()'/></td> <td><input type='button' value='Abbrechen' onclick='infowindow.close()'/></td> </tr>";
      infowindow = new google.maps.InfoWindow({
          content: html
    });
    poiMode = false;
    infowindow.open(map, marker);
    google.maps.event.addListener(marker, 'click', function(event){
        clickOnMarker(marker);
    });
    $('.activeButton').removeClass("activeButton");

    }
}

saveData() 函数位于同一个 .js 文件中,如下所示:

     function saveData() {
      var name = escape(document.getElementById("name").value);
      var address = escape(document.getElementById("address").value);
      var latlng = marker.getPosition();

      var url = base_url + "map/addMarker?name=" + name + "&address=" + address + "&description=" + "test" + "&user_id=" + "1" +"&lat=" + latlng.lat() + "&lng=" + latlng.lng() + "&date=" + "" +"&type=" + "poi";
      downloadUrl(url, function(data, responseCode) {
        if (responseCode == 200 && data.length <= 1) {
          infowindow.close();
          document.getElementById("message").innerHTML = "Location added.";
        }
      });
}

然而,当我按下“表单”上的保存按钮时,我收到控制台错误:

Uncaught ReferenceError: saveData is not defined

你能告诉我,我做错了什么吗?因为我看不到自己的错误。 提前致谢

【问题讨论】:

  • 只是为了让人们不必四处寻找,saveData 在第一个函数 (createMarker) 的字符串化 HTML 的最后一行中被调用,作为内联 onclick 处理程序对于&lt;input&gt;
  • saveData 是全局的还是在另一个函数中作用域?您可以尝试将您的函数定义为窗口对象的属性。而不是function saveData(),而是window.saveData = function()
  • 检查 InfoWindow 是否没有在 iframe 中呈现。
  • saveData() 函数在$(document).ready(function() {...}); 的范围内,将函数超出此范围会导致更多错误。

标签: javascript function undefined


【解决方案1】:

我通过将 saveData() 定义为 infowindow 的函数解决了这个问题

infowindow.saveData = function(){
...
});

并调用函数:

<input type='button' value='Save' onclick='infowindow.saveData()'/>

感谢 basilikum 和其他所有人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2023-01-23
    • 2016-11-03
    • 2011-01-05
    • 2016-01-02
    • 2013-10-06
    • 2016-12-17
    相关资源
    最近更新 更多