从另一个脚本替换Rails中的脚本中的值
问题描述:
我在我的show.html.erb
从另一个脚本替换Rails中的脚本中的值
<script type="text/javascript">
$(document).ready(function(){
$("#map<%= shop.id %>").gMap({ markers: [{ latitude: <%= shop.lat %>,
longitude: <%= shop.lng %>,
html: "<%=h shop.name %><br/><a href='http://maps.google.com/maps?q=<%=h shop.address_geo %>' target='_blank'>See Full Map</a>" },
],
zoom: 16 });
</script>
中间有这个。然后在它的底部,我想做这样的事情:
function append_place(type) {
var shop_id = id;
$("shop_id").append(shop_id);
}
我希望用<%= shop.id %>
替换id
。我知道这两个脚本在编码时都是错误的,但我该如何实现呢?不能使用Rails cos这是客户端和JSON。它首先从数据库中提取id
,然后通过内联脚本附加它。
谢谢。
答
,你可以放弃该变量为任意数量的地方,但在保持全局命名空间干净的兴趣,你可以创建一个myPageVars对象在jQuery对象,例如:
<script type="text/javascript">
$.myPageVars = $.myPageVars || {}; // don't want to overwrite any previously written vars
$.myPageVars.shop_id = "<%= shop.id %>"
$(document).ready(function(){
$("#map<%= shop.id %>").gMap({ markers: [{ latitude: <%= shop.lat %>,
longitude: <%= shop.lng %>,
html: "<%=h shop.name %><br/><a href='http://maps.google.com/maps?q=<%=h shop.address_geo %>' target='_blank'>See Full Map</a>" },
],
zoom: 16 });
</script>
和你的追加功能看起来像这样:
function append_place(type) {
var shop_id = $.myPageVars.shop_id || false;
if (shop_id) $("#map"+shop_id).append(whatever);
}
对不起,如果我误解你的问题,我不是一个铁轨用户。
'var shop_id = $ .myPageVars.shop_id ||假;似乎打破所有的代码。 – Victor 2010-11-02 07:36:01