jquery在尝试获取textbox(asp.net)的值时返回'UNDEFINED'

Arp*_*pta 4 asp.net jquery jquery-ui

当我试图通过.val()获取文本框的值时,它总是显示"未定义".以下是我正在使用的代码.请帮帮我的朋友.

<asp:TextBox runat="server" ID="txtComment" Width="650" />

<asp:Button runat="server" ID="btnSubmitComment" Text="Submit" />

$(document).ready(function () {
    $('input[id$=btnSubmitComment]').click(function () {
    var comment = $("#txtComment").val();
    alert(comment);
});
Run Code Online (Sandbox Code Playgroud)

Joe*_*tch 5

asp.net填充元素的返回id.看起来您在click语句中使用它.你试过这个吗?

$(document).ready(function () {
    $('input[id$=btnSubmitComment]').click(function () {//you are using the [id$=] selector here.
    var comment = $("input[id$=txtComment]").val();//use it here too
    alert(comment);
});
Run Code Online (Sandbox Code Playgroud)

或者你可以使用<%= txtComment.ClientID %>,但我个人从未使用它.我相信你需要告诉asp.net将JavaScript文件嵌入到页面中,以便客户端id放置在适当的位置.我将寻找这个潜在解决方案的链接.