JQuery Hide Element and isVisible Issue

I am basically trying to do form validation. Evertyhing works great. Except one. Firstly, here is my code:

        $('#submit_btn').click(function(){  

        $("input:not([type=radio],[type=checkbox])").each(function() {
                if ($(this).val() == ""){
                 $('#'+$(this).attr("name")+'_error').show("");
                }
                else{
                   $('#'+$(this).attr("name")+'_error').hide("normal");
                }

            });
            if(!($("div[id$=error]").is(":visible")))
                               alert("a");

        });

After clicking the submit button, it checks the input, which is not a switch or flag. And if the input is empty, it shows an error.

If any input is entered, the error becomes hidden.

In the end, I check if any error message is displayed if I do not submit the form.

My problem: I hide the error message with a little animation with .hide ("normal"). Therefore, I believe that in the process of hiding the last error message, my last if statement is executed, and he believes that there is a visible error message (however, it was in the process of hiding)

How can I execute the if statement after the hide process is complete?

, , .

, . .

!

+3
2

:visible , , . :

$('#submit_btn').click(function(){  
    var error = false;

    $("input:not([type=radio],[type=checkbox])").each(function() {
        if ($(this).val() == ""){
            $('#'+$(this).attr("name")+'_error').show(400);
            error = true;
        }
        else{
            $('#'+$(this).attr("name")+'_error').hide(400);
        }
    });
    if(error)
        alert("a");

});
+5

- .hide("normal"), " " .hide(). . .

. :

$('#submit_btn').click(function(){  
  $("input:not([type=radio],[type=checkbox])").each(function() {
    if ($(this).val() == ""){
      $('#'+$(this).attr("name")+'_error').show("");
    }
    else{
      $('#'+$(this).attr("name")+'_error').hide("normal", function() {

        // This function will be executed when the "normal" animation is complete

        if(!($("div[id$=error]").is(":visible")))
          alert("a");

      });
    }
  });
});

2 ... , , , , , .

0

Source: https://habr.com/ru/post/1786935/


All Articles