jQuery Jquery隐藏字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2027062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:37:18  来源:igfitidea点击:

Jquery Hidden Field

asp.netjqueryhidden-field

提问by Dooie

Why can't I get the value of this hidden field?

为什么我无法获取这个隐藏字段的值?

I have a control...

我有控制...

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

Which renders as...

这呈现为...

<input type="hidden" name="ctl00$cph_main$HiddenFieldServerDateTime" id="ctl00_cph_main_HiddenFieldServerDateTime" value="08/01/2010 10:54:11" 

Which I'm trying to get the value of using...

我试图获得使用的价值...

var serverDateTime = $("#HiddenFieldServerDateTime").attr('value');

So what's wrong?

那么怎么了?

I prefer this

我更喜欢这个

var dateTime = $("[id$=_HiddenFieldServerDateTime]").val();

回答by Darin Dimitrov

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:

因为 jQuery 对asp:HiddenField. 它在 HTML 结构中查找您拥有<input type="hidden" name="ctl00$cph_main$HiddenFieldServerDateTime" id="ctl00_cph_main_HiddenFieldServerDateTime" .... 所以没有输入ID= HiddenFieldServerDateTime。有几种方法可以克服这个问题:

  • Use a css selector:

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

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

    CssClassis not an available class on the HiddenFieldclass (and it doesn't have an Attributescollection, so you can't add it manually).

  • Use ClientIDproperty:

    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();
    
  • 使用 css 选择器:

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

    使用以下选择器: var serverDateTime = $(".SomeStyle").val();

    CssClass不是该类上的可用类HiddenField(并且它没有Attributes集合,因此您无法手动添加它)。

  • 使用ClientID属性:

    var serverDateTime = $("#<%= HiddenFieldServerDateTime.ClientID %>").val();
    
  • 将隐藏字段包裹在您可以选择的内容中:

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

     

    var serverDateTime = $('.date-time-wrap input[type=hidden]').val();
    

回答by CodeClimber

I know this has already been answered and resolved but here are two better (in my opinion) and simpler alternatives. If you are using .NET4 (or above) you can use ClientIDMode="Static" to force your ID to be used in the generated HTML:

我知道这已经得到了回答和解决,但这里有两个更好(在我看来)和更简单的替代方案。如果您使用 .NET4(或更高版本),您可以使用 ClientIDMode="Static" 强制在生成的 HTML 中使用您的 ID:

<asp:HiddenField ID="HiddenFieldServerDateTime" runat="server" ClientIDMode="Static" />

which means you can do this in your JQuery:

这意味着您可以在 JQuery 中执行此操作:

var serverDateTime = $('#HiddenFieldServerDateTime').val();

or if you want to use the css class route then use a normal ASP:TextBox (which has a CssClass attribute) but just don't display it:

或者,如果您想使用 css 类路由,请使用普通的 ASP:TextBox(具有 CssClass 属性)但不显示它:

<asp:TextBox ID="HiddenFieldServerDateTime" runat="server" style="display:none" CssClass="MyStyle"></asp:TextBox>

which allows you to do this:

这允许你这样做:

var serverDateTime = $('.MyStyle').val();

Note that the css class you use doesn't have to be actually declared anywhere. You can just use it as a marker.

请注意,您使用的 css 类实际上不必在任何地方声明。您可以将其用作标记。

回答by Rozwel

I just ran into a similar issue and my answer was to make a new control which inherits from HiddenFieldand gives it a CssClassproperty:

我刚刚遇到了类似的问题,我的答案是创建一个新控件,该控件继承自HiddenField并赋予它一个CssClass属性:

public class HiddenFieldWithClass : HiddenField
{
    [CssClassProperty]
    [DefaultValue("")]
    public virtual string CssClass 
    {
        get
        {
            string Value = this.ViewState["CssClass"] as string;
            if (Value == null)
                Value = "";
            return Value;
        }
        set
        {
            this.ViewState["CssClass"] = value;
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (this.CssClass != "")
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
        }
        base.Render(writer);
    }
}

I am now able to assign a class to my hidden fields and use the class value to find the correct field on the client side.

我现在可以为我的隐藏字段分配一个类,并使用类值在客户端找到正确的字段。

Might also be worth noting that in my case the hidden fields are being created dynamically in code behind, the above may need some enhancments to be usable in the designer.

可能还值得注意的是,在我的情况下,隐藏字段是在后面的代码中动态创建的,上述内容可能需要一些增强功能才能在设计器中使用。

Hope this helps someone else along the way.

希望这可以帮助其他人。

回答by developerdoug

This will work as well by using jQuery to select all IDs that end with _HiddenFieldServerDateTime.

这也可以通过使用 jQuery 选择所有以 _ HiddenFieldServerDateTime结尾的 ID 来工作。

var myVal = $("[id$='_HiddenFieldServerDateTime']").val();

回答by Matthew O'Leary

<input type="hidden" ID="HiddenFieldServerDateTime" runat="server" class="HiddenFieldServerDateTime" />

<input type="hidden" ID="HiddenFieldServerDateTime" runat="server" class="HiddenFieldServerDateTime" />

回答by Joberror

Add a class attribute ".myHiddenValue" to the tag then use

将类属性“.myHiddenValue”添加到标签然后使用

var myVal = $(".myHiddenValue").val()

or since this will render after loading the document my advise use this

或者因为这会在加载文档后呈现我的建议使用它

$(document).ready(function(){
   var myVal = $("input[name='ctl00$cph_main$HiddenFieldServerDateTime']").val();
 }
);

Note: also applies for the first example as well