2013-03-05 101 views
1

我有一个<asp:menu/>控制和隐藏field.Now我使用jQuery来更改隐藏字段的值。 代码是: -如何获得隐藏的字段值后面的代码更新由jquery/javascript

$(function() { 

    $(".primaryStaticMenu tr,td").each(function(index) { 

     $(this).click(function() { 

      if ($(this).attr("title") != "undefined" 
       && $(this).attr("title").length > 0) { 

       document.getElementById('ctl00_Hidden_Master_Location').value = $(this).attr("title"); 

       alert(document.getElementById('ctl00_Hidden_Master_Location').value); 
       //return false; 
      } 
     }); 
    }); 
}); 

服务器端代码以获取更新的值是: -

string Get_cng_value = Hidden_Master_Location.Value; 

Hidden_Master_Location.Value显示null每次。 任何人都可以告诉我如何从后面的代码中获取隐藏字段的更新值。

+2

请为hidde提供aspx页面代码n字段 – 2013-03-05 06:26:00

+0

此行在浏览器中提示什么? alert(document.getElementById('ctl00_Hidden_​​Master_Location')。value); – 2013-03-05 06:27:02

+0

我是jquery的新手,但隐藏字段的id是'ctl00_Hidden_​​Master_Location',不应该用它来代替Hidden_​​Master_Location.Value在服务器代码中引用它吗? – Aashray 2013-03-05 06:28:57

回答

1

让我们说你的隐藏字段是..

<asp:HiddenField ID="Hidden_Master_Location" runat="server" /> 

你可以得到的jQuery中隐藏的申请作为

var locationValue= $("#<%= Hidden_Master_Location.ClientID %>").val(); 
+0

它在客户端工作正常,我需要从服务器端代码访问更新的值。并且我无法获得该值。 – 2013-03-05 07:33:13

+0

@ ashish,确保您的隐藏值在页面加载时不会被清除,回发。 – sharad 2013-03-05 07:53:40

+0

我把这个代码放在表单加载上,但是这个值是空的。 代码: - 字符串Get_cng_value = Hidden_​​Master_Location.Value; – 2013-03-05 08:12:06

0

做到这一点的价值,它适用于me.the诀窍是将隐藏的字段宝贵ID保存在另一个隐藏的输入字段中,然后使用该隐藏值将其重新构建。

标记

<asp:HiddenField ID="HiddenFieldMaster" runat="server" /> 
    <input type="hidden" id="inputHidden" value='<%= HiddenFieldMaster.ClientID%>' /> 

Javascript

$(function() { 

$(".primaryStaticMenu tr,td").each(function(index) { 

    $(this).click(function() { 

     if ($(this).attr("title") != "undefined" 
      && $(this).attr("title").length > 0) { 

      var inputHidden = document.getElementById('inputHidden'); 
       $("#" + inputHidden.value).val($(this).attr("title")); 

      alert(inputHidden.value); 
      //return false; 
     } 
    }); 
}); 
}); 

代码隐藏

String Get_cng_value = HiddenFieldMaster.Value; 
+0

对不起,但它不工作。 隐藏字段在简单的aspx页面上正常工作。 但是,在这里我的母版页包含这个隐藏的字段,并在母版页加载事件隐藏的字段值清空。而我无法访问该值。 – 2013-03-05 10:45:22

+0

很抱歉听到这个消息。你必须调查你的页面生命周期中发生了什么。尝试一个隐藏的CSS类的文本框,看看它的价值是否被消灭。 – 2013-03-05 11:02:40

+0

我试着用文本框控制,但问题仍然存在。 文本框的值在页面加载时被擦除。 – 2013-03-05 11:56:41