简体   繁体   English

Javascript字符串意外变成对象

[英]Javascript String Unexpectedly Turning Into An object

I've been working on a simple project involving Google Maps API. 我一直在从事一个涉及Google Maps API的简单项目。 My friend wants a visitor map for his website where visitors can place a mark representing their location. 我的朋友想要他的网站的访客地图,访客可以在其中放置代表其位置的标记。 These marks are also associated with data such as a name, a message etc. This data should be displayed in the InfoWindow when the mark is clicked upon. 这些标记还与名称,消息等数据相关联。单击标记时,这些数据应显示在信息窗口中。

So far its all working pretty good, but I am running into a bug that has stumped me. 到目前为止,所有功能都运行良好,但是我遇到了一个让我感到困惑的错误。

When a user puts down a new point a function is called to generate a form specific for that object. 当用户放下新点时,将调用一个函数来生成特定于该对象的表单。

function setupForm(id){
    var content='<div class="gdform">'+
            '<form id='+id+'>'+
            '<ul>'+
            '<li><input type="text" id="userName" value="name" onfocus="value=\'\'"/></li>'+
            '<li><textarea rows ="15" cols="50" wrap="hard"></textarea></li>'+
            '</ul>'+
            '<input type="button" value="submit" onclick="markers.'+id+'.setData()"/>'+
            '<input type="button" value="cancel" onclick="setTimeout(function(){removeMark('+id+')}, 100)"/>'+
            '</form>'+
            '</div>';
return content;

}

Where id is the unique id for every mark on the map. id是地图上每个标记的唯一ID。 The marks are stored in a hash table and can be reference with this id. 这些标记存储在哈希表中,并且可以使用此ID进行引用。

The problem here is coming from the cancel button. 这里的问题来自取消按钮。 When pressed, it should close the InfoWindow, remove the marker from the map, and delete the marker from the the hash table. 当按下时,它应该关闭InfoWindow,从地图上删除标记,并从哈希表中删除标记。 To do this it calls the function removeMark, which looks like this: 为此,它调用了removeMark函数,如下所示:

function removeMark(id){
   infoWindow.close()
   markers.id.mark.setMap(null);
   delete markers.id;

}

Here however, I receive a type error telling me that it can't read property mark of undefined. 但是,在这里,我收到一个类型错误,告诉我它无法读取未定义的属性标记。

In an attempt to debug the code I set a few some breakpoints, one in the setupForm function and the other in removeMark. 为了调试代码,我设置了一些断点,一个在setupForm函数中,另一个在removeMark中。 When stepping through the code I noticed that while in setupForm , the js console identifies id as a string. 逐步执行代码时,我注意到在setupForm ,js控制台将id为字符串。 When in removeMark however, the js console informs me that id has suddenly become an object. 但是,在removeMark中时,js控制台会通知我id突然成为对象。

I haven't been able to figure out why this should be. 我还不能弄清楚为什么会这样。 Here is all the code: 这是所有代码:

http://jsfiddle.net/39vKm/ http://jsfiddle.net/39vKm/

Thanks for the help! 谢谢您的帮助!

Alright... there are three problems with your code... 好的...您的代码存在三个问题...

first of all, you're missing single quotations around the id in the onclick declatation on the cancel button. 首先,您在取消按钮的onclick声明中缺少ID周围的单引号。 Second, you need to return false from the removeMark function and return removeMark in the onclick event on the button... (there is no need for the timeout) 其次,您需要从removeMark函数返回false,并在按钮上的onclick事件中返回removeMark(不需要超时)

CODE: 码:

function setupForm(id){
    var content='<div class="gdform">'+
                '<form id='+id+'>'+
                '<ul>'+
                '<li><input type="text" id="userName" value="Whats your name?" onfocus="value=\'\'"/></li>'+
                '<li><textarea rows ="15" cols="50" wrap="hard"></textarea></li>'+
                   '</ul>'+
                '<input type="button" value="submit" onclick="markers.'+id+'.setData()"/>'+
                '<input type="button" value="cancel" onclick="return removeMark(\''+id+'\')"/>'+
                '</form>'+
                '</div>';
    return content;

}

Third, when removing the mark, you need to reference the marker using brackets on the array... 第三,删除标记时,您需要使用阵列上的括号来引用标记...

function removeMark(id){
    infoWindow.close()
    markers[id].mark.setMap(null);
    delete markers[id];
    return false;
}

Updated fiddle: http://jsfiddle.net/gislikonrad/39vKm/5/ 更新的小提琴: http : //jsfiddle.net/gislikonrad/39vKm/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM