【问题标题】:Why on change event for TextArea not firing in my ASP.net page为什么在我的 ASP.net 页面中没有触发 TextArea 的更改事件
【发布时间】:2014-08-28 18:43:16
【问题描述】:

我的 asp.net 页面中有以下代码:

<asp:UpdatePanel runat="server" ClientIDMode="Static" ID="upTaskDetailRight" UpdateMode="Conditional">
    <ContentTemplate>
        <div style="width: 98%; padding-bottom: 10px;" class="brClear">
            <div class="noteClass brClear">Notes</div>
            <div style="width: 98%; height: 120px;">
                <textarea id="taskNotes" runat="server" class="taskNotes" style="width: 100%; height: 100%; scrollbar-base-color: #A0A0A0; scrollbar-base-color: #A0A0A0; scrollbar-3dlight-color: #A0A0A0; scrollbar-highlight-color: #A0A0A0; scrollbar-track-color: #EBEBEB; scrollbar-arrow-color: #FFFFFF; scrollbar-shadow-color: #A0A0A0; scrollbar-darkshadow-color: #A0A0A0;"></textarea>
            </div>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="btnComplete" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

翻译成这个 HTML 源代码:

<div id="upTaskDetailRight">
    <div style="width: 98%; padding-bottom: 10px;" class="brClear">
        <div class="noteClass brClear">Notes</div>
        <div style="width: 98%; height: 120px;">
            <textarea name="ctl00$ContentMain$taskNotes" id="ContentMain_taskNotes" class="taskNotes" style="width: 100%; height: 100%; scrollbar-base-color: #A0A0A0; scrollbar-base-color: #A0A0A0; scrollbar-3dlight-color: #A0A0A0; scrollbar-highlight-color: #A0A0A0; scrollbar-track-color: #EBEBEB; scrollbar-arrow-color: #FFFFFF; scrollbar-shadow-color: #A0A0A0; scrollbar-darkshadow-color: #A0A0A0;"></textarea>
        </div>
    </div>
</div>

我有以下 JQuery:

$(function () {
    $("body").on('change', "#ContentMain_taskNotes", function (e) {
        alert("changing");
    });
});

我希望它做的是,每次我对 TextArea 进行任何更改时,它都应该向我显示 changing 警报,但是当我在 TextArea 中键入或删除任何内容时什么都没有发生。

我做错了什么,我该如何解决?

【问题讨论】:

  • change 之外添加keyup 的处理程序。

标签: jquery html asp.net updatepanel


【解决方案1】:

改为绑定input propertychange。这将在每次更改时发出警报。 change 事件只会在离开 textarea 时触发。

$(function () {
    $("body").on('input propertychange', "#ContentMain_taskNotes", function (e) {
        alert("changing");
    });
});

JSFiddle.

【讨论】:

  • 谢谢。我会测试一下。
  • 它在 asp.net 页面之外工作,但在其中,它对我不起作用:/
  • 如果你将生成的 HTML 标记直接放在你的 aspx 中,它会起作用吗?
  • 让我试试看。谢谢。
【解决方案2】:

您可以尝试这样的方法来绑定多个选项

 $("#ContentMain_taskNotes").bind('blur change keyup',function (e) {
        alert("changing");
    });

of if just for keyup then this

 $("#ContentMain_taskNotes").bind('keyup',function (e) {
        alert("changing");
    });

JSflddle Demo

【讨论】:

    【解决方案3】:

    change 事件在失去焦点时触发,而不是在按下某个键时触发。如果您想“在线”检测更改,则需要使用关键相关事件。

    $("body").on('keyup', "#ContentMain_taskNotes", function (e) {
        alert("changing");
    });
    

    DEMO

    【讨论】:

    • 我在从母版页获取 TextArea 的值时确实遇到了问题,这是我的问题:stackoverflow.com/questions/25555640/…。这是我做的一个测试,但对我不起作用:/谢谢。我会测试一下
    猜你喜欢
    • 2014-09-02
    • 2011-06-19
    • 2017-09-25
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2019-04-02
    • 2021-09-28
    • 1970-01-01
    相关资源
    最近更新 更多