javascript - 在页面加载和随后的 ajax 回发时注册 jquery

标签 javascript jquery ajax asp.net-ajax updatepanel

使用应该在第一页加载和随后的 Ajax 回发时注册的 JQuery。 应用此控件的控件位于更新面板内。现在我在做什么

document.readysys.application.add.load 中以两种方式注册相同的函数,因此它适用于更新面板内的控件和控件在更新面板之外。

$(document).ready(function () {

    CheckMaxlength();

    //If Text area is placed inside update panel then apply restriction for texarea size.

    Sys.Application.add_load(function () {

    CheckMaxlength();

    });
});

我想知道使用更新面板内部和外部更新面板的控件的确切方式是什么

最佳答案

我不确定你想做什么,但我怀疑delegated events可能是你的答案。

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers.

例如,您可以向所有文本区域添加功能,而不管它们是何时添加到页面中的

$( "body" ).on( "click", "textarea", function() {
    alert( $( this ).val() );
});

您的情况的第一个好处是任何时候添加到更新面板的任何文本区域都将获得委派的功能。

此外,如果您想针对更新面板内和更新面板外的文本设置不同的行为,您也可以这样做。

$("body").on('click', 'textarea', function () {
    alert( "outside" + $(this).val());
});

$(".update-panel").on('click', 'textarea', function () {
    alert("inside: " +  $(this).val());

    //stop the event from propagating up to the body
    event.stopPropagation(); 
});

另请注意,运行此代码时,只有 bodyupload-container 需要准备就绪。它不需要文本区域在那里,因此您不必在 Sys.Application.add_load 中运行它。

关于javascript - 在页面加载和随后的 ajax 回发时注册 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32781260/

相关文章:

php - 如果我的选项卡内容在各处都相同,为什么我的 JQuery 脚本似乎只在开始浏览我的选项卡之前才起作用?

javascript - 卡在 Angular 2 的 Angular Material 中的 MdSnackBar 的可观察循环中

javascript - TypeScript 错误 - 应有 1-2 个参数,但有 0 个或更多。 TS2556

javascript - 在 Ionic 2 中集成 JavaScript Web 插件

javascript - 使用 ng-angular 动态创建的元素不会触发 onClick 事件

javascript - 使用以下操作刷新网页

jquery - Magento 2 token 身份验证 - 400 错误/CORS 问题

javascript - 使用 getCell jqgrid 获取 html 值

Javascript 刽子手问题

jquery定时改变item类