我尝试使用ajax jasonp来获得经度和经度 有了这个: -
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script>
$(document).ready(function(){
var region = "Rajkot,Jamnagar";
var cn ="IN";
var lat;
var lng;
var array = region.split(',');
var lat = new Array(), lng = new Array();
var totalLength = array.length;
var count = 0;
for(var item in array)
{
$.ajax({
url: "http://services.gisgraphy.com//geocoding/geocode?address="+array[item]+"&country="+cn+"&format=json",
async: false,
dataType:'jsonp',
success: function(data){
count++;
lat.push(data.result[0].lat);
lng.push(data.result[0].lng);
if (count == totalLength) {
var commaSeperatedLat = lat.toString();
localStorage.setItem("newlat",commaSeperatedLat);
var commaSeperatedLong = lng.toString();
console.log(commaSeperatedLat);
}
}
});
}
function all()
{
var moodle_groups_id = (localStorage.getItem("newlat"));
alert(moodle_groups_id);
}
});
</script>
<body>
<input type="button" value="ok" onclick="all()"></input>
</body>
我得到了所有纬度和经度
但是现在我想要点击按钮/时的变量
我使用localStorage.setItem和getItem
请帮我解决这个问题。
感谢。
答案 0 :(得分:0)
你只需要在$(document).ready(function()之外传递函数all() 功能;)
<script>
$(document).ready(function(){
var region = "Rajkot,Jamnagar";
var cn ="IN";
var lat;
var lng;
var array = region.split(',');
var lat = new Array(), lng = new Array();
var totalLength = array.length;
var count = 0;
for(var item in array)
{
$.ajax({
url: "http://services.gisgraphy.com//geocoding/geocode?address="+array[item]+"&country="+cn+"&format=json",
async: false,
dataType:'jsonp',
success: function(data){
count++;
lat.push(data.result[0].lat);
lng.push(data.result[0].lng);
if (count == totalLength) {
var commaSeperatedLat = lat.toString();
localStorage.setItem("newlat",commaSeperatedLat);
var commaSeperatedLong = lng.toString();
console.log(commaSeperatedLat);
}
}
});
}
});
function all()
{
var moodle_groups_id = (localStorage.getItem("newlat"));
alert(moodle_groups_id);
}
</script>