使用jquery asp.net在ContentTemplate中查找控件

时间:2011-07-12 23:42:34

标签: jquery asp.net controls find createuserwizard

我正在使用jquery来查找ContentTemplate中的文本框控件。我一直收到错误:

  

当前上下文中不存在名称“txtUserName”

这是我的javascript:

            function ShowAvailability() {
            var myvar = $('#<%=txtUserName.ClientID %>').text();
            $.ajax({
                type: "POST",
                url: "Register.aspx/CheckUserName",
                data: '{userName: "' + $(myvar)[0].value + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response);
                }
            });

这是我的标记:

    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="False" OnCreatedUser="RegisterUser_CreatedUser">
    <WizardSteps>
        <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
            <ContentTemplate>
                <div class="accountInfo">
                  <fieldset class="register">
                    <div>
                        UserName :
                        <asp:TextBox ID="txtUserName" runat="server" onkeyup="ShowAvailability()"></asp:TextBox>
                        <input id="btnCheck" type="button" value="Show Availability" onclick="ShowAvailability()" />
                        <br />
                        <span id="mesg"></span>
                    </div>

请帮忙。我似乎无法在任何地方找到解决方案。谢谢!

1 个答案:

答案 0 :(得分:0)

不要试图选择它,而是让它作为参数出现:

function ShowAvailability(domObject) {
            var myvar = domObject.val(); //switched from text() to val()
            $.ajax({
                type: "POST",
                url: "Register.aspx/CheckUserName",
                data: '{userName: "' + $(myvar)[0].value + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response);
                }
            });

然后在标记中使用jquery $(this)让文本框将自己的引用发送给函数:

<asp:TextBox ID="txtUserName" runat="server" onkeyup="ShowAvailability($(this))"></asp:TextBox>