jQuery asp.net 组件上的点击处理程序未激活

jQuery on click handler not activated on asp.net component

我正在使用一些 jQuery.

开发 C#/Asp 应用程序

我想要一个特定的函数,每次单击具有 class(例如 "loadingInProgress")的组件时都会调用该函数。但是,下面的代码似乎不起作用,我不确定为什么。我尝试使用 ID,使用 CssClass 而不是 class 并使用 .click() 而不是 .on('click', ...) 无济于事。

这是按钮代码

    <asp:Button ID="btn_Rechercher" Text="Rechercher" runat="server"  class="loadingInProgress"
                   OnClick="btn_Rechercher_Click" />

还有我的.Js.

    $(document).ready(function () {

    $('.loadingInProgress').on('click', function (e){

    /* OVERLAY ON AJAX    */
    $(document).ajaxStart(function () {
        displayOverlayInProgress();
    });

    $(document).ajaxComplete(function (event, xhr, settings) {
        removeOverlayInProgress();
    });          
  });
    });
     function displayOverlayInProgress() {
     $('.overlay').show();
     $('body').css('cursor', 'wait');
    }

    function removeOverlayInProgress() {
    $('.overlay').hide();
    $('body').css('cursor', 'auto');

    }      

嗯,看来你没试过OnClientClick

asp:Button ID="btn_Rechercher" Text="Rechercher" runat="server"  class="loadingInProgress" 
OnClick="btn_Rechercher_Click" 
OnClientClick="loadingInProgress"/>


function loadingInProggress() {

    displayOverlayInProgress(); // don't wait for ajaxStart


    $(document).ajaxComplete(function (event, xhr, settings) {
        removeOverlayInProgress();
    });   
}

试试这个

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

Html:

   <asp:Button ID="btn_Rechercher" Text="Rechercher" runat="server"  class="loadingInProgress"
                       OnClick="btn_Rechercher_Click" />

Jquery:

 $("#btn_Rechercher").click(function () {

 $(document).ajaxStart(function () {
        displayOverlayInProgress();
    });

    $(document).ajaxComplete(function (event, xhr, settings) {
        removeOverlayInProgress();
    });  

    });

试试这个:

<asp:Button ID="btn_Rechercher" Text="Rechercher" runat="server"  CssClass="loadingInProgress"
               OnClick="btn_Rechercher_Click" />

问题是你不应该运行 .ajaxStart.ajaxComplete 每次点击按钮。 您应该在 .ready 上实现这两个回调,但在 .click 事件中您应该只调用 .ajax.load.get 或任何其他将启动请求