2013-03-05 92 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