Get the Value of an asp:HiddenField using jQuery

后端 未结 8 2239
慢半拍i
慢半拍i 2020-12-15 16:36

I have two pages. From the first page, I open a modal with a querystring that holds that value of a client name. I then use this to set a hiddenfield on the modal that opene

相关标签:
8条回答
  • 2020-12-15 16:49

    You forgot the # in your selector to select by ID:

    var hv = $('#hidClientField').val();
    

    Although asp.net generates ID based on the naming containers so you might end up with an ID like ctl1$hidClientField. You can then use the "attribute ends with" selector:

    var hv = $('input[id$=hidClientField]').val();
    

    Check the documentation about jQuery selectors

    0 讨论(0)
  • 2020-12-15 16:51

    you can do in this way

    var hval = $('#<%= hdMarkupPercentage.ClientID%>').val();
    
    0 讨论(0)
  • 2020-12-15 16:52

    Use ID selector.

    var hv = $('#hidClientName').val();
    

    Or

    var hv = $('#<%=hidClientName.ClientID%>').val();
    
    0 讨论(0)
  • 2020-12-15 16:56

    Include ClientIDMode="Static" in your code.

    var obj = $('#hidClientName').val(); <asp:HiddenField ID="hidClientName" ClientIDMode="Static" runat="server" />

    Or

    var obj = $('#<%=hidClientName.ClientID%>').val(); <asp:HiddenField ID="hidClientName" runat="server" />

    0 讨论(0)
  • 2020-12-15 17:01

    Because jQuery knows nothing about asp:HiddenField. It looks in the HTML structure where you have <input type="hidden" name="ctl00$cph_main$HiddenFieldServerDateTime" id="ctl00_cph_main_HiddenFieldServerDateTime" .... So there's no input with ID= HiddenFieldServerDateTime. There are a few ways to overcome this:

    • Use a css selector:

      <asp:HiddenField ID="HiddenFieldServerDateTime" 
                       runat="server" 
                       CssClass="SomeStyle" />
      

      with the following selector: var serverDateTime = $(".SomeStyle").val();

      CssClass is not an available class on the HiddenField class (and it doesn't have an Attributes collection, so you can't add it manually).

    • Use ClientID property:

      var serverDateTime = $("#<%= HiddenFieldServerDateTime.ClientID %>").val();
      
    • Wrap the hidden field in something you can select:

      <div class="date-time-wrap">
        <asp:HiddenField ID="..." runat="server" />
      </div>
      

       

      var serverDateTime = $('.date-time-wrap input[type=hidden]').val();
      
    0 讨论(0)
  • 2020-12-15 17:03

    Try any of the following

    1. If ASP.Net control and javascript both are on same page, then use

      var hv = $("#"+ '<%= hidClientField.ClientID %>').val();
      
    2. If you want to access the control from some JS file, then

      // 'id$' will cause jQuery to search control whose ID ends with 'hidClientField'
      var hv = $('input[id$=hidClientField]').val();
      
    3. You can use class name selector to achieve same. Check out this similar question.

    In asp.net, controls id is mangled. Because of this your code is not working.

    Hope this works for you.

    0 讨论(0)
提交回复
热议问题