Jquery textarea .val() 意外返回未定义
Jquery textarea .val() returning undefined unexpectedly
下面的代码已经运行了几个星期,当我在 jquery 中使用选择器获取文本区域的值时,它开始偶尔返回 'undefined',如下所示:
var text = escape($(this).find('textarea[id*=txtText]').val());
此控件在 HTML 中由以下内容适当定义:
<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
Rows="5" Columns="50" CssClass="customCss" ClientIDMode="Static" />
我之前通过在我的 jquery 中调用以下命令来访问文本:
var text = escape($("#txtText").val());
但这也开始返回 undefined,这就是我将其更改为前面示例的原因。我很困惑为什么这项工作的某些实例,而其他人则没有。作为一个多行文本框,jquery 可能由于任何
而将其声明为未定义
<br/>
文本区域中包含的字符?
如有任何帮助或建议,我们将不胜感激。
问题是您正在执行 runat="server"
,这会使服务器更改 ID。这个新 ID 的示例可能类似于 MainContent_txtText
,您可以通过右键单击文本框并单击 Inspect
检查元素来确定服务器将 ID 更改为什么。
Chrome 内置了开发人员工具,您可以使用它进行检查。如果你不在Chrome,Firefox也有Inspector,或者你可以下载使用Firebug。
你可以做一些事情来规避这个...
解决方案 1
使用第一段中的 Inspector 方法确定新生成的 ID,并以与您的代码示例类似的方式通过该 ID 访问它:
var text = escape($("#insert_id_from_inspector_here").val());
解决方案 2
给控件一个 class,并从那个 class 元素中获取值:
var text = escape($(".txtText").val());
解决方案 3
您还可以将 ID 动态打印到您的 javascript 代码中:
var text = escape($("#<%= txtText.ClientID %>").val());
这可能是因为您 asp:TextBox 您已将 ClientIDMode 指定为静态。
<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
Rows="5" Columns="50" CssClass="customCss" **ClientIDMode="Static"** />
如果此控件包含在其他控件中,您最终会得到如下内容:
ListView1_ProductIDLabel_txtText
Static The ClientID value is set to the value of the ID property. 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.
使用Chrome开发工具从DOM
中查看id
您可以做的一件事是...
var text = escape($(this).find('textarea[id*=<%=txtText.ClientID%>]').val());
性能差异可以忽略不计,即便如此我通常只是查看源代码,找到 w/e asp.net 正在调用它并使用它。据我所知,上述方法是万无一失的。
下面的代码已经运行了几个星期,当我在 jquery 中使用选择器获取文本区域的值时,它开始偶尔返回 'undefined',如下所示:
var text = escape($(this).find('textarea[id*=txtText]').val());
此控件在 HTML 中由以下内容适当定义:
<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
Rows="5" Columns="50" CssClass="customCss" ClientIDMode="Static" />
我之前通过在我的 jquery 中调用以下命令来访问文本:
var text = escape($("#txtText").val());
但这也开始返回 undefined,这就是我将其更改为前面示例的原因。我很困惑为什么这项工作的某些实例,而其他人则没有。作为一个多行文本框,jquery 可能由于任何
而将其声明为未定义<br/>
文本区域中包含的字符?
如有任何帮助或建议,我们将不胜感激。
问题是您正在执行 runat="server"
,这会使服务器更改 ID。这个新 ID 的示例可能类似于 MainContent_txtText
,您可以通过右键单击文本框并单击 Inspect
检查元素来确定服务器将 ID 更改为什么。
Chrome 内置了开发人员工具,您可以使用它进行检查。如果你不在Chrome,Firefox也有Inspector,或者你可以下载使用Firebug。
你可以做一些事情来规避这个...
解决方案 1
使用第一段中的 Inspector 方法确定新生成的 ID,并以与您的代码示例类似的方式通过该 ID 访问它:
var text = escape($("#insert_id_from_inspector_here").val());
解决方案 2
给控件一个 class,并从那个 class 元素中获取值:
var text = escape($(".txtText").val());
解决方案 3
您还可以将 ID 动态打印到您的 javascript 代码中:
var text = escape($("#<%= txtText.ClientID %>").val());
这可能是因为您 asp:TextBox 您已将 ClientIDMode 指定为静态。
<asp:TextBox ID="txtText" runat="server" MaxLength="150" alt="150" TextMode="MultiLine"
Rows="5" Columns="50" CssClass="customCss" **ClientIDMode="Static"** />
如果此控件包含在其他控件中,您最终会得到如下内容:
ListView1_ProductIDLabel_txtText
Static The ClientID value is set to the value of the ID property. 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.
使用Chrome开发工具从DOM
中查看id您可以做的一件事是...
var text = escape($(this).find('textarea[id*=<%=txtText.ClientID%>]').val());
性能差异可以忽略不计,即便如此我通常只是查看源代码,找到 w/e asp.net 正在调用它并使用它。据我所知,上述方法是万无一失的。