Jquery send event and return false

Hi, I am checking an empty field on the form and warning the user. but when the user warns, he publishes the data, I can not return false, so as not to refresh the page

$('#loginAccount').submit(function() {
    $(this).find(':input:text').each(function(i) {
        if($(this).val()=="") {
            // alert($('label').eq(i).html())
            $('#alert3').html('Please fill all fields.');
            return false;
        }
    });
});
+3
source share
4 answers
$('#loginAccount').submit(function() {
    var valid = true;
    $(this).find(':input:text').each(function(i) {
        if($(this).val() == "") {
            // alert($('label').eq(i).html())
            $('#alert3').html('Please fill all fields.');
            valid = false;
        }
    });
    return valid;
});

You are currently returning from everyone. What you need to do is keep track of whether it is valid and then use this value as a return from your shipment.

+7
source

return false; jQuery each(). , . , , (), , .

+5

You need to return false in submit, not in the function each:

$('#loginAccount').submit(function() {
    var isValid = true;
    $(this).find(':input:text').each(function(i) {
        if($(this).val()=="")
        {
            isValid = false;
            //alert($('label').eq(i).html())
            $('#alert3').html('Please fill all fields.');

        }
    });
    return isValid;
});
+1
source

Maybe you use closure to return a value?

    $('#loginAccount').submit(function() {
           var result = true;    
            $(this).find(':input:text')
                 .each(function(i) {
                if($(this).val()=="")
                {
                    //alert($('label').eq(i).html())
                    $('#alert3').html('Please fill all fields.');
                    result = false;
                }
            });
            return result;
            })
0
source

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


All Articles