如何在从另一个javascript函数调用的javascript函数中传递多个参数?

时间:2012-12-05 13:00:10

标签: google-maps-markers javascript

我正在使用谷歌地图,点击标记时打开的信息窗口会调用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()

2 个答案:

答案 0 :(得分:2)

在这一行

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

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

例如,如果是城市,那么它应该是一个字符串:

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

答案 1 :(得分:1)

在这一行:

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

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

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

并将其提交到onClick="setStyles(params)" ...

的函数

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

setStyles范围内,您可以使用某些参数访问每个参数。比如params.custID或者你所谓的参数对象的members,比如....

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