1 回答
TA贡献1798条经验 获得超3个赞
zero你得到值的原因是因为你lookupGridcodeByLatAndLong在第一次调用你的函数时传递了你的lat和lang值,这两个值都将返回 undefined 因为你还没有输入它们。
你必须用值填充它们,然后在你进行计算之前点击按钮来获取lat和值,实际上你不需要将你的对象值解析为参数,因为你将它初始化为全局参数,所以你仍然可以在你的函数中使用它而不解析它。langGRIDCODE
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" id="my-form">
<div class="form-group">
<label for="">Enter Lat</label>
<input type="text" id="lat" step="any">
<label for="">Enter Long</label>
<input type="textr" id="long" step="any">
</div>
<input type="button" id="Btn" value="Calculate" >
<h2 id="result">result</h2>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
const
myData = {
datagrid: [
{ "GRIDCODE": 1735,"lat": 35.91511132, "long": -5.401484264 },
{ "GRIDCODE": 1805, "lat": 35.90677815, "long": -5.468149593 },
{ "GRIDCODE": 1808, "lat": 35.90677815, "long":-5.459816427 },
{ "GRIDCODE": 1784, "lat": 35.89844499, "long":-5.476482759 }
]
};
function lookupGridcodeByLatAndLong(lat, long){
let gridcode = 0;
var result = myData.datagrid.find(x => x.lat == lat && x.long == long)
if (result) {
gridcode = result.GRIDCODE;
}
return gridcode;
}
$("#Btn" ).click(function() {
var lat = document.getElementById("lat").value,
long = document.getElementById("long").value;
var result = lookupGridcodeByLatAndLong(lat, long)
$("#result").html(result);
});
</script>
</body>
</html>
添加回答
举报