0

我正在使用谷歌地图,点击标记打开的信息窗口调用 JavaScript 函数。
必须从此函数调用另一个具有多个参数的函数。

以下是我的代码:

function createMarker(point,custid,streetadd,city,state,zip,address,phone,website,co) {         
    var infowindowHover,infowindowClick;

    var hoverText = "<CENTER><B>" + co + "</B></CENTER>";
    var markerMarkup = "<TABLE><TR><TD colspan='2'><B>";
    markerMarkup = markerMarkup + co + "</B></TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + streetadd + "</TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + city + "," + state + " " + zip + "</TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + phone + "</TD></TR><TR><TD colspan='2'>";
    if(website.indexOf("http://")>0) {
        markerMarkup = markerMarkup +"<a href=";
    } else {
        markerMarkup = markerMarkup +"<a href=http://";
    }
    markerMarkup = markerMarkup + website + " target=_blank>" + website + "</a></TD></TR><TR><TD>";                                
    var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;
    markerMarkup = markerMarkup + "<input type='button' class='button' value='see available styles' id='styles' onclick='setstyles("+ funCall +");'></input>";
    //markerMarkup = markerMarkup + "<input type='button' class='button' value='see available styles' id='styles' onclick='setstyles("+ custid +");'></input>"; 

    markerMarkup = markerMarkup + "</TD></TR></TABLE>";

    var marker = new google.maps.Marker({
        position: point,
        map: map,
        icon: image             
    });

    google.maps.event.addListener(marker, "mouseover", function () {
        if (infowindowHover) infowindowHover.close();
        infowindowHover = new google.maps.InfoWindow({content: hoverText});
        infowindowHover.open(map, marker);
    });

    google.maps.event.addListener(marker, "mouseout", function () {
        if (infowindowHover) infowindowHover.close();                
    });  

    google.maps.event.addListener(marker, "click", function () {
        if (infowindowClick) infowindowClick.close();
        infowindowClick = new google.maps.InfoWindow({ content: markerMarkup });
        infowindowClick.open(map, marker);
    });

    //google.maps.event.addListener(marker, "mouseout", function () {
    //    if (infowindowClick) infowindowClick.close();                
    //});
    return marker;
}

我想从此函数调用的第二个函数是:

function setstyles(idcust,streetadd,city,state,zip,address,phone,website,co){
    var msg= "This feature is  available only to logged-in ";    
    alert(idcust);                     
    <%if Session("ctype")="1" then %>
        alert(msg + " non wholesalers.");
    <%else %>           
        <%if Session("ctype")="0" then %>
            var storestyles = 'storestyles.asp?id=' + idcust;         
            document.getElementById('storeaddresses').style.display = 'block';             
        <%else %>
             alert(msg + "users."); 
        <% end if %>
    <%end if %>
}

我想将参数从 传递createMarker()setstyles()目前我无法做到的。

4

2 回答 2

2

在这一行

var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;

如果任何参数是字符串,则应该将它们包装成引号。

例如,如果 city 应该是一个字符串:

var funCall = custid + "," + streetadd + ",\"" + city + "\"," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;
于 2012-12-05T13:41:21.960 回答
1

在这一行:

    var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;

您只需构建一个string包含以逗号分隔的变量的巨大变量。因此,在这种情况下,您也只提交了一个参数(the string)。尝试将所有参数包装成一个参数对象,如下所示:

     var params = {
        custID: custid,
        streetAdd: streetadd,
        [...]
     }

并将其提交给onClick="setStyles(params)"...的功能

的签名setStyles应如下所示: function setStyles(params){...}

setStyles您可以使用 sth 访问每个参数。喜欢params.custID或任何你称之为members参数对象的东西......

还要考虑下面评论中给出的提示,例如使用+=被认为是良好的编码风格的问题......

于 2012-12-05T13:23:21.290 回答