简体   繁体   English

jQuery textarea .val()意外返回未定义

[英]Jquery textarea .val() returning undefined unexpectedly

The following code has been working for a few weeks and has started occasionally returning 'undefined' when I use a selector in jquery to get the textarea's value, like so: 下面的代码已经运行了几周,当我在jquery中使用选择器来获取textarea的值时,偶尔会开始返回“ undefined”,如下所示:

var text = escape($(this).find('textarea[id*=txtText]').val());

This control is appropriately defined below in the HTML by the following: 下面通过以下方式在HTML中适当定义了此控件:

<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
     Rows="5" Columns="50" CssClass="customCss" ClientIDMode="Static" />

I had previously accessed the text by calling the following in my jquery: 我以前通过在jquery中调用以下内容来访问文本:

var text = escape($("#txtText").val());

But this had started returning undefined as well, which is why I changed it to the prior example. 但这也开始返回undefined,这就是为什么我将其更改为先前的示例。 I'm confused as to why some instances of this work, and others do not. 我很困惑为什么有些工作实例,而有些却没有。 Being a multiline textbox, could jquery be declaring it as undefined due to any 作为多行文本框,jquery可能由于任何原因将其声明为undefined

<br/>   

characters that are included in the textarea? textarea中包含的字符?

Any help or suggestions would be greatly appreciated. 任何帮助或建议,将不胜感激。

The problem is that you're doing a runat="server" which makes the server change the ID. 问题是您正在执行runat="server" ,这会使服务器更改ID。 An example of this new ID could be something like MainContent_txtText , you can determine what the server is changing the ID to by right-clicking the textbox and clicking Inspect to inspect the element. 例如,可以使用MainContent_txtText这样的新ID作为示例,您可以通过右键单击文本框并单击“ Inspect以检查元素来确定服务器将ID更改为什么。

Chrome has Developer Tools built in which allows you to inspect. Chrome具有内置的开发人员工具,可让您进行检查。 If you are not in Chrome, Firefox also has an Inspector, or you can download and use Firebug. 如果您不在Chrome中,则Firefox也具有Inspector,或者您可以下载并使用Firebug。

You could do a few things to circumvent this... 您可以做一些事情来规避这个问题...

Solution 1 解决方案1

Determine the newly-generated ID using the Inspector method in the first paragraph and access it via that ID in similar fashion to your code sample: 使用第一段中的Inspector方法确定新生成的ID,并以类似于代码示例的方式通过该ID访问它:

var text = escape($("#insert_id_from_inspector_here").val());

Solution 2 解决方案2

Give the control a class, and get the value from that class element: 给控件一个类,并从该类元素获取值:

var text = escape($(".txtText").val());

Solution 3 解决方案3

You can also print out the ID dynamically into your javascript code: 您还可以将ID动态地输出到javascript代码中:

var text = escape($("#<%= txtText.ClientID %>").val());

It could be cause on your asp:TextBox You have specified ClientIDMode of static. 这可能是由于您的asp:TextBox指定的ClientIDMode为static。

<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
 Rows="5" Columns="50" CssClass="customCss" **ClientIDMode="Static"** />

If this control is contained in other controls you'd end up with something like: 如果此控件包含在其他控件中,您将得到类似以下内容的结果:

ListView1_ProductIDLabel_txtText ListView1_ProductIDLabel_txtText

MSDN Details on ClientIDMode 有关ClientIDMode的MSDN详细信息

Static The ClientID value is set to the value of the ID property. 静态ClientID值设置为ID属性的值。 If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains. 如果控件是命名容器,则该控件将用作其包含的任何控件的命名容器层次结构的顶部。

Use Chrome dev tools to look at the id from the DOM 使用Chrome开发者工具查看DOM中的ID

One thing you can do is... 您可以做的一件事是...

var text = escape($(this).find('textarea[id*=<%=txtText.ClientID%>]').val());

Performance difference is negligible, even so I just usually do a view source, find w/e asp.net is calling it and use that. 性能差异可以忽略不计,即使如此,我通常只是做一个视图源,发现w / e asp.net正在调用它并使用它。 The above approach is fool proof however as far as I know. 据我所知,上述方法是万无一失的。

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

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