【发布时间】:2017-10-07 08:59:34
【问题描述】:
我想在模板的onRendered 函数中包含一个使用jquery 的脚本。
这是代码
Template.Admin_users.onRendered(function(){
var instance = this;
instance.autorun(function(){
if($){
console.log($);
console.log("jquery");
$("#Users.collection-item").click();
$.getScript(
"http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"
//'https://maps.googleapis.com/maps/api/js?libraries=places'
, function(){
console.log("loaded gmap");
var acs = $(".citycomplete").length;
for(var i = 0 ;i<acs;i++){
var autocomplete = new google.maps.places.Autocomplete($(".citycomplete")[i], {});
autocomplete.id = $(".citycomplete")[i].id;
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
$("input[name=lat]#"+autocomplete.id).val(place.geometry.location.lat());
$("input[name=lng]#"+autocomplete.id).val(place.geometry.location.lng());
});
}
});
}
});
});
问题是jquery(和gmap)不会在页面刷新时加载。如果我更改模板(加载另一条路线)然后回到这个Admin_users 模板,jquery 和 gmap 将加载。如何在页面加载时加载 jquery?
我在这里看到了很多关于这个问题的问题,但似乎没有一个对我有用(solution onetwothreefour)
【问题讨论】:
标签: javascript jquery templates meteor