2012-01-29 72 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

相关问题