2012-01-29 69 views
1

我正在使用jQuery在隱藏字段中設置一些值,這些值是完全設置的。如何在不點擊提交按鈕的情況下在隱藏字段中獲取值

但問題是隱藏的字段不會顯示值,直到我提交表單。

在提交表單之前是否有值得的值。

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script> 
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$("#hdnCountryCode").val(geoip_country_code()); 
$("#hdnCountyName").val(geoip_country_name()); 
$("#hdnCity").val(geoip_city()); 
$("#hdnRegionCode").val(geoip_region()); 
$("#hdnRegion").val(geoip_region_name()); 
$("#hdnLatitude").val(geoip_latitude()); 
$("#hdnLongitude").val(geoip_longitude()); 
}); 
</script> 

<body> 
<form id="form1" runat="server"> 
<asp:HiddenField runat="server" ID="hdnCountryCode" /> 
<asp:HiddenField runat="server" ID="hdnCountyName" /> 
<asp:HiddenField runat="server" ID="hdnCity" /> 
<asp:HiddenField runat="server" ID="hdnRegionCode" /> 
<asp:HiddenField runat="server" ID="hdnRegion" /> 
<asp:HiddenField runat="server" ID="hdnLatitude" /> 
<asp:HiddenField runat="server" ID="hdnLongitude" /> 
<asp:Button runat="server" ID="btnV" Text="s" onclick="btnV_Click" /> 
<% 
Google.Values Send = new Google.Values(); 
Send.CountryName = hdnCountyName.Value; 
Send.CountryCode = hdnCountryCode.Value; 
Send.RegionName = hdnRegion.Value; 
Send.RegionCode = hdnRegionCode.Value; 
Send.City = hdnCity.Value; 
Send.Latitude = hdnLatitude.Value; 
Send.Longitude = hdnLongitude.Value; 
%> 
</form> 
</body> 

當傳遞給我的類的屬性時,隱藏字段的值上面的代碼給我「」。但是當我使用按鈕單擊事件相同的代碼返回我,我需要

+1

如果你想檢索代碼的價值,你可以用'$( 「#hdnCountryCode」)VAL()'(沒有傳遞參數'.VAL()',它返回當前值)。你是這個意思嗎?還是你想問如何調試你的代碼? – nnnnnn 2012-01-29 12:02:36

+0

你需要爲此使用AJAX。在填充字段後的'$(document).ready()'中,添加AJAX請求,將這些值發送到服務器,然後您可以在服務器端C#代碼中使用它們。 – 2012-01-29 12:03:10

+0

ShadowWizard可以給我一些解釋,說明你剛剛說的請求 – user1174458 2012-01-29 12:06:32

回答

0

爲了得到你需要得到呈現ID值的所有值,這就好比做:

$("#<%=hdnCountryCode.ClientID%>").val(geoip_country_code()); 
0

你可以看到這個代碼:

<head> 
    <title>Test</title> 
    <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script> 
    <script language="JavaScript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var countryCode = geoip_country_code(); 
      var countryName = geoip_country_name(); 
      $('#countryCode').html(countryCode); 
      $('#countryName').html(countryName); 
     }); 
    </script> 
</head> 
<body> 
    <p> 
     CountryCode: <span id="countryCode">countryCode</span> 
    <p> 
    <p> 
     CountyName: <span id="countryName">CountyName</span> 
    <p> 
</form> 
</body> 
</html> 
+1

嘗試解釋您的代碼的作用以及它與問題中的代碼有何不同。 – 2012-09-17 14:47:51

+0

沒有任何解釋,很難找到像這樣的答案的用途。 – 2012-09-21 21:08:00

相關問題