简体   繁体   English

当可见更改为false时,为什么JQuery无法获取Textarea值?

[英]Why JQuery is unable to get Textarea value when the visible changed to false?

I thought changing textarea to non visible will disappear from user view but still be hidden with stored information? 我以为将textarea更改为不可见将从用户视图中消失,但仍被存储的信息隐藏? For my result, as long the textarea remain visible, the Jquery was able to get information. 对于我的结果,只要文本区域保持可见,Jquery就能获取信息。 C# server side code is the one who change textarea visible to false. C#服务器端代码是将textarea可见更改为false的代码。

After I click the first button(BtnIPAM), the C# server side get information from textarea and do the job then change textarea visible to false. 单击第一个按钮(BtnIPAM)之后,C#服务器端从textarea获取信息并执行此工作,然后将可见的textarea更改为false。

Then I click the second button(BtnISM) to call JQuery to get same textarea, but it get nothing. 然后,我单击第二个按钮(BtnISM)来调用JQuery以获取相同的文本区域,但没有任何结果。 If I comment out in C# server side to leave textarea visible, the JQuery will be able to get information from textarea. 如果我在C#服务器端注释掉以使textarea可见,则JQuery将能够从textarea获取信息。

Anyone know why this happen and a way to fix it? 有人知道为什么会发生这种情况以及解决方法吗?

aspx codes included textarea, first button(BtnIPAM), and second button(BtnISM): aspx代码包括textarea,第一个按钮(BtnIPAM)和第二个按钮(BtnISM):

    <asp:TextBox ID="txt" runat="server" visible="False" TextMode="MultiLine" 
                Width=356px Height=200px style="margin-left: 0px"></asp:TextBox>

<asp:Button ID="BtnIPAM" runat="server" onclick="BtnIPAM_Click" 
            Text="Assign It!" />
        <br />
        <asp:Button ID="BtnISM" runat="server" 
            OnClientClick="if (!CreateIsm()) {return false;}"  UseSubmitBehavior="false"
            Text="ISM Easy Button" />

Simple C# button clicked method, 简单的C#按钮点击方法,

protected void BtnIPAM_Click(object sender, EventArgs e)
{
   //other codes are doing job before change txt to false
   txt.Visible = false;
}

In javascript CreateIsm() function, I use notes = $('#txt').val(); 在javascript CreateIsm()函数中,我使用notes = $('#txt').val(); to get textarea info. 获取textarea信息。

在asp.net中,当您将可见设置为错误控制时,不在页面上呈现,这就是为什么您无法在页面上访问它的原因

In ASP.NET server-side code, setting Visible to false will not actually make the control invisible, but will not render it on the page instead. 在ASP.NET服务器端代码中,将Visible设置为false实际上不会使控件不可见,而是将其呈现在页面上。

If you want to render a hidden control, you can write: 如果要呈现隐藏的控件,可以编写:

txt.Style.Add(HtmlTextWriterStyle.Display, "none");

Or simply: 或者简单地:

txt.Style.Add("display", "none");

The code txt.Visible = false; 代码txt.Visible = false; causes your textarea HTML to not be rendered at all. 导致您的textarea HTML根本无法呈现。 You can use txt.Attributes["style"] = "display:none"; 您可以使用txt.Attributes["style"] = "display:none";

<asp:TextBox> is probably creating an alternate ID on the page for your control, so searching for '#txt' won't get the actual control. <asp:TextBox>可能正在页面上为您的控件创建备用ID,因此搜索'#txt'将无法获得实际的控件。 Alternatively you can set ClientIDMode to ClientIDMode.Static (just by specifying Static for that property on the control), then it wont change the ID on you, but you have to ensure all controls on your page have unique IDs. 或者,您可以将ClientIDMode设置为ClientIDMode.Static (只需通过为控件上的该属性指定Static即可),那么它就不会更改您的ID,但是您必须确保页面上的所有控件都具有唯一ID。

Like this: 像这样:

<asp:TextBox ID="txt" runat="server" ClientIDMode="Static">

Additionally, setting Visible='false' on the control will not even write the markup to display it on the page, so the control doesn't exist. 此外,在控件上设置Visible='false'甚至不会编写标记以将其显示在页面上,因此该控件不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM