简体   繁体   English

JQuery等到元素隐藏后再继续

[英]JQuery wait until an element has hidden before continuing

My Jquery code is as follows: 我的Jquery代码如下:

if($('#LoginFormError').is(':visible'))$('#LoginFormError').slideUp();
//Check inputs and run ajax
if(CheckInput('#LoginUsername') && CheckInput('#LoginPassword')){
    $('#LoginSubmit').attr('disabled', true);
    $.ajax({
        (..ajax stuf..)
    });
}else{
    $('#LoginFormError').html("Please fill in everything.");
    $('#LoginFormError').slideDown();
}

For the purpose of this, CheckInput returns true if the field is filled in. 出于此目的,如果填写了字段,CheckInput将返回true。

What I want to do is if then #LoginFormError is visible, for the script to hide the element and then run the check procedures. 我想要做的是,如果然后#LoginFormError可见,脚本隐藏元素然后运行检查程序。 The issue is though that #LoginFormError may not be visible at all, so I can't put everything into the call back from the SlideUp function, although I don't think I can. 问题是虽然#LoginFormError可能根本看不到,所以我不能把所有内容都放到SlideUp函数的回调中,尽管我认为不行。

Since the callback will always be called (even if the element is already hidden) there's no need to check. 由于将始终调用回调(即使元素已被隐藏),也无需检查。 Just put everything in the callback: 把所有内容都放在回调中:

$('#LoginFormError').slideUp(400, function{
        //Check inputs and run ajax
        if(CheckInput('#LoginUsername') && CheckInput('#LoginPassword')){
            $('#LoginSubmit').attr('disabled', true);
            $.ajax({
                (..ajax stuf..)
            });
        }else{
            $('#LoginFormError').html("Please fill in everything.");
            $('#LoginFormError').slideDown();
        }
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM