推迟执行连接到控制事件的Javascript,以支持外部jQuery函数

Postpone the execution of Javascript wired to a control event in favour of external jQuery function

本文关键字:支持 外部 函数 jQuery Javascript 连接 执行 控制 事件 推迟      更新时间:2023-09-26

我有一个由另一个开发人员开发的ASP.NET Web Form应用程序。

此开发人员广泛使用ASP.NET控件,因此有许多自动生成的Javascript函数连接到HTML元素的事件。

现在我被要求添加一个功能:在第一次点击时禁用提交按钮,以避免用户多次点击该按钮。

这是我使用的jQuery代码http://jsfiddle.net/2hgnZ/80/HTML DOM与我使用的相同:

$('#form1').submit(function(e) {
    $(this).find('input[type=submit]').attr('disabled', 'disabled');
    // this is to prevent the actual submit
    e.preventDefault();
    return false;
});

我的代码中的这个脚本不起作用,经过多次尝试,我确信它依赖于连接到按钮的onSubmit事件的JavaScript事件。

我怎样才能推迟这个Javascript的执行,以支持jQuery不引人注目的函数?

hmm。你能确认你的活动正在启动吗?我检查了提交的文档,它是绑定的,而不是实时的,这很好。

把电线接到按钮上怎么样?这并不完美,因为回车键不使用按钮,但如果它以这种方式工作,你会学到一些

您是否尝试过调用e.stopImmediatePropagation();http://api.jquery.com/event.stopImmediatePropagation/

这段代码延迟了所需函数量的执行。

// delay code for one second
$(document).ready(function() 
{
    window.setTimeout(function() 
    {
        // delayed code goes here
    }, 1000);
}); 

这段代码应该可以帮助您获得原始事件,然后在您想要的代码之后触发它。

$(function(){
    var $button = $('#actionButton'),
    clickEvent = $button.data("events")['click'][0].handler; //saves the original click event handler
    $button.unbind('click'); //and removes it from the button
    //create a new click event.
    $button.click(function(e){
        // doWhatever we need to do
        $.ajax({
            url: url,
            data: data,
            success: function(){
                $.proxy(clickEvent,$button)(e);//attach the original event to the button
            }
        });
    });
});