【问题标题】:Getting JavaScript Error in master page using ASP.NET WEB Form使用 ASP.NET WEB 表单在母版页中获取 JavaScript 错误
【发布时间】:2016-12-06 13:35:15
【问题描述】:

Java Script 在使用 ASP.NET C# 的内容页面中不起作用,我正在尝试计算下面多行文本框的字符是字符计数代码。 低于错误。

加载资源失败:服务器响应状态为 404 (Not Found) Uncaught ReferenceError: txtComments is not defined(…)

下面是我的代码JS和设计代码。

    <script type="text/javascript">

       function characterCounter(controlId, countControlId, maxCharlimit) {
           if (controlId.value.length > maxCharlimit)
               controlId.value = controlId.value.substring(0, maxCharlimit);
           else countControlId.value = maxCharlimit - controlId.value.length;
       }


   </script>
    <fieldset style="width: 280px;">
        <legend>Counting Remaining Characters example</legend>
        <asp:TextBox ID="txtComments" Width="280px" Rows="4" Columns="12" runat="server"
            TextMode="MultiLine" onkeyup="characterCounter(txtComments, this.form.remCount, 150);"
            onkeydown="characterCounter(txtComments, this.form.remCount, 150);" /><input type="text"
                name="remCount" size="3" maxlength="3" value="150" readonly="readonly" />
        characters left
    </fieldset>

当我尝试输入一些东西时,值不会改变。

【问题讨论】:

    标签: javascript c# asp.net


    【解决方案1】:

    您需要传递this 而不是txtComments

    <asp:TextBox ID="txtComments" Width="280px" Rows="4" Columns="12" runat="server"
                TextMode="MultiLine" onkeyup="characterCounter(this, this.form.remCount, 150);"
                onkeydown="characterCounter(this, this.form.remCount, 150);" />
    

    【讨论】:

    • @the upvoter : 他忘了通过,请编辑答案
    • 但您在回答中没有提及,请更新您的回答。 @豹
    • @MMK 我在 asnwer 中没有提到什么?
    • @Leopard :不要表现得像个聪明人。放聪明点。请记住,现在只有您编辑了答案并将txtComments 替换为this。您必须补充的一点是,该方法需要 TextBox 的 ID,而您正在传递整个 TextBox。
    • @un-lucky 如果你看到function OP 不是通过 id 访问它,而是直接使用它。
    【解决方案2】:

    我建议您使用this 代替文本框ID,因为该ID 将根据页面名称(以及母版页名称,如果有的话)发生变化,因为它是一个服务器端控件。这意味着调用方法将如下所示:

    onkeydown="characterCounter(this, this.form.remCount, 150);"
    

    附加说明:由于您是通过 javascript 方法访问文本框的值,因此您无需传递文本框的 ID,而是可以将其作为 TextBox 传递。所以传递参数将是this 而不是this.Id。并且不得不感谢Leopard 指出这一点;

    【讨论】:

    • 我在一个页面中有五个多行文本框,我必须检查所有和你的意思 this.txtComments,我必须保留。 @un-lucky
    • 每个文本框都有不同的 id,对吧?所以this.id 会给你相应的ID。它不是`this.txtComments`,而是this.id
    • 然后上面再回答一个答案,只有这意味着混淆。 @un-lucky
    • 当我按下然后只更改低于计数值,如果我将复制并过去然后不改变为什么? @un-lucky
    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 2015-01-03
    • 2014-07-11
    • 2012-06-24
    • 2013-11-25
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多