【问题标题】:Find control within ContentTemplate using jquery asp.net使用 jquery asp.net 在 ContentTemplate 中查找控件
【发布时间】:2011-10-04 02:11:51
【问题描述】:

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

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

【问题讨论】:

  • 你设置PerformSubstituion=true;了吗?
  • 那条线去哪儿了?抱歉,我是 javascript 新手。
  • &lt;%=txtUserName.ClientID %&gt; 不会在 JS 文件中进行评估。您的脚本在哪里定义(在 aspx 或 JS 文件中)?
  • @Mrchief:我的脚本在 aspx 文件中

标签: jquery asp.net controls find createuserwizard


【解决方案1】:

与其试图选择它,不如让它作为参数来找你:

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>

【讨论】:

  • 非常感谢!我终于摆脱了那个错误。但是我的 javascript 并没有按照预期的方式运行。这部分有问题:“ $(myvar)[0].value ”。如果我将其更改为“$(myvar)[0]”或“$(myvar).value”,我会得到不同的结果。无论哪种方式,我都没有得到正确的结果。你认为语法正确吗?
  • 好的,我知道了。该部分应该只是“myvar”(不带引号且不带 $)
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2010-09-14
  • 2010-09-06
  • 2012-03-14
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多