[英]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.