触发('click')完成触发后执行操作?

时间:2013-03-14 18:19:40

标签: javascript jquery sharepoint

我管理SharePoint网站并且对底层代码的访问权限有限;但是,我可以通过JavaScript加载页面后操作。一直在使用jQuery和SPServices。

我有一个我要添加到表单的自定义按钮;它会覆盖页面上的标准“保存”按钮,因此我可以在保存/关闭之前执行其他一些ajax操作。这很好用:

$('#the_save_button').hide();

$("#my_custom_save_button").click(function() {
    // do some custom stuff first
    $('#the_save_button').trigger('click'); // 
    window.close();
}

项目保存并window.close()触发。大!但是,现在,我宁愿不关闭窗口,而是设置window.location.href ...但是,当我这样做时,页面将在 trigger('click')完成之前重定向开火和我的物品没有保存:

$('#the_save_button').hide();

$("#my_custom_save_button").click(function() {
    // do some custom stuff first
    $('#the_save_button').trigger('click'); // 
    window.location.href = "/my/new/url.aspx";
}

我已尝试使用window.setTimout,但$('#the_save_button').trigger('click')本身有内置重定向...

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

是的,只需让您的点击处理程序返回一个延迟对象,并使用它来决定何时重定向。

$("#the_save_button").click(function(e){
    e.preventDefault();
    return $.ajax({...});
});

现在你可以这样做:

$("#my_custom_save_button").click(function() {
    // do some custom stuff first
    $('#the_save_button').triggerHandler('click').done(function(){
        window.location.href = "/my/new/url.aspx";
    });

});

注意:在这种情况下使用triggerHandler代替trigger非常重要,triggerHandler允许您使用返回的jqXHR对象。