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

发布于 2024-11-24 02:46:39 字数 1598 浏览 2 评论 0原文

我正在使用 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>

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

I am using jquery to find a textbox control which is inside a ContentTemplate. I keep getting the error:

The name 'txtUserName' does not exist in the current context

This is my 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);
                }
            });

And this is my markup:

    <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>

Please help. I can't seem to find the solution anywhere. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-12-01 02:46:39

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

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>

Instead of trying to select it, let it come to you as a parameter:

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);
                }
            });

Then in the markup use the jquery $(this) to have the text box send a reference of itself to the function:

<asp:TextBox ID="txtUserName" runat="server" onkeyup="ShowAvailability($(this))"></asp:TextBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文