[英]How do I get my hidden field values on Postback
这是我的Java脚本
$('#geoLocation').click(function () {
alert("I am Here");
if (navigator.geolocation) {
// Get Latitude and Longitude
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
// Hide Locator Panel, if Browser not supported
document.getElementById('panel1').style.display = 'none';
}
});
function showPosition(position) {
// x.innerHTML = "Latitude: " + position.coords.latitude +
// "<br>Longitude: " + position.coords.longitude;
document.getElementById('latitude').innerHTML = position.coords.latitude;
document.getElementById('longitude').innerHTML = position.coords.longitude;
}
我需要在CodeBehind上的回发中获取经度和纬度值(隐藏在屏幕上)。 我该怎么做呢?
我假设“纬度”和“经度”是页面上的HTML元素?
在这种情况下,您应该能够向它们添加runat =“ server”属性,然后它们将在服务器端代码中可用。 如果您的网站是ASP.NET 4 / 4.5,则还可以添加ClientIDMode =“ Static”,并且showPosition函数将按原样运行; 如果不是,您可以给他们一个类,并使用jQuery类选择器来获取两个项目。
您可以使用$('#HiddenFieldId').val()
在客户端将数据分配给HiddenField 。
<div id="geoLocation">Click me to retrieve Geo Location</div>
<asp:HiddenField runat="server" ID="LatitudeHiddenField" />
<asp:HiddenField runat="server" ID="LongitudeHiddenField" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$('#geoLocation').click(function () {
if (navigator.geolocation) {
// Get Latitude and Longitude
navigator.geolocation.getCurrentPosition(showPosition, geoLocationErrors);
} else {
// Hide Locator Panel, if Browser not supported
document.getElementById('panel1').style.display = 'none';
}
function showPosition(position) {
$('#<%= LatitudeHiddenField.ClientID %>').val(position.coords.latitude);
$('#<%= LongitudeHiddenField.ClientID %>').val(position.coords.longitude);
}
function geoLocationErrors() {
alert("Your browser doesn't support Geo Location.");
}
});
</script>
然后,您可以从服务器端的HiddenField检索这些值,方法是-
var latitute = LatitudeHiddenField.Value;
var longitude = LongitudeHiddenField.Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.