jQuery access input hidden value

前端 未结 9 1827
执念已碎
执念已碎 2020-11-28 01:50

How can I access tag\'s value attribute using jQuery?

相关标签:
9条回答
  • 2020-11-28 02:51

    Watch out if you want to retrieve a boolean value from a hidden field!

    For example:

    <input type="hidden" id="SomeBoolean" value="False"/>
    

    (An input like this will be rendered by ASP MVC if you use @Html.HiddenFor(m => m.SomeBoolean).)

    Then the following will return a string 'False', not a JS boolean!

    var notABool = $('#SomeBoolean').val();
    

    If you want to use the boolean for some logic, use the following instead:

    var aBool = $('#SomeBoolean').val() === 'True';
    if (aBool) { /* ...*/ }
    
    0 讨论(0)
  • 2020-11-28 02:53

    To get value, use:

    $.each($('input'),function(i,val){
        if($(this).attr("type")=="hidden"){
            var valueOfHidFiled=$(this).val();
            alert(valueOfHidFiled);
        }
    });
    

    or:

    var valueOfHidFiled=$('input[type=hidden]').val();
    alert(valueOfHidFiled);
    

    To set value, use:

    $('input[type=hidden]').attr('value',newValue);
    
    0 讨论(0)
  • 2020-11-28 02:53

    If you have an asp.net HiddenField you need to:

    To access HiddenField Value:

    $('#<%=HF.ClientID%>').val()  // HF = your hiddenfield ID
    

    To set HiddenFieldValue

    $('#<%=HF.ClientID%>').val('some value')   // HF = your hiddenfield ID
    
    0 讨论(0)
提交回复
热议问题