我有一个项目,我需要根据从数据库中获取的 longlat 值在 map (谷歌地图)上显示标记。我成功地在表上显示了 longlat 值,但没有在 map 上显示,因为 javascript 调用即使在简单的警报下也不起作用。我为此使用了 struts2 迭代器,并且在迭代期间放置了 javascript。这是我的代码。为简洁起见省略了一些代码。谁能帮我这个?
function plotOnMap(lat,long) {
alert(long + "," + lat);
}
.
.
.
// code for struts iteration
<s:iterator value="myBeanList" status="myBeanStatus" var="roamerBean" >
<tr>
<s:if test="#myBeanStatus.even == true">
<s:if test="#myBeanStatus.first == true">
<td style="background: #CCCCCC" onshow="javascript:plotOnMap(<s:property value="#roamerBean.latitude" />,<s:property value="#roamerBean.longitude" />)"><s:property value="#roamerBean.cellId"/></td>
<td style="background: #CCCCCC"><s:property value="#roamerBean.cellName"/></td>
<td style="background: #CCCCCC"><s:property value="#roamerBean.longitude"/></td>
<td style="background: #CCCCCC"><s:property value="#roamerBean.latitude" /></td>
</s:if>
</s:if>
</tr>
</s:iterator>
最佳答案
嗯,我想 onshow
event绝对不是您认为的那样:
onshow : User requested the element be shown as a context menu.
想像一下,您要做的是在将鼠标悬停在表格的行上时调用 plotOnMap
...然后简单地使用迭代器和众所周知的 onmouseover
event :
onmouseover : User moved pointer into boundaries of element or one of its descendant elements.
还要避免内联样式,并将其放在头部(或更好地放在 CSS 中)以减少代码中的噪音:
<style>
tr.ccc > td {
background-color: #CCCCCC;
}
</style>
正文:
<s:iterator value="myBeanList">
<tr class = "ccc"
onmouseover = "javascript:plotOnMap(<s:property value='latitude' />,<s:property value='longitude' />);">
<td><s:property value="cellId" /></td>
<td><s:property value="cellName" /></td>
<td><s:property value="longitude" /></td>
<td><s:property value="latitude" /></td>
</tr>
</s:iterator>
编辑
yes, tried that and it's working, thanks. I can now plot the longlat with markers on the map. What i'm struggling now is tracking which of the values are the start and end points so that i can show via the info window.
我不知道信息窗口 是什么,但是您可以随时通过 s:property 指向第一个元素来获取起始纬度和经度。例如:
alert('Starting latitude : <s:property value="%{myBeanList[0].latitude}"/>' );
alert('Starting longitude : <s:property value="%{myBeanList[0].longitude}"/>' );
关于java - Struts2 - 在 JSP 迭代期间调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929229/