在 jquery 中停止表单提交 blur ajax 以显示错误

stop form submit in jquery blur ajax for showing error

我想要的是 -username 必须在 blur 函数中验证并且如果 "username already exists" 则表单不得提交。 示例 - https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail&hl=en 当用户名退出时我必须停止提交 AJAX 侧模糊功能

这是我的js代码

(function ($) {
    var u_error;
    console.log('data');
    $('#username').blur(function() {
        if($("#username").val() != '') {
            $.ajax({
                url: "<?php echo base_url('welcome/un_availability') ?>",
                data: 'un=' + $("#username").val(),
                type: "POST",
                success: function (data) {
                    console.log(data);
                    if (data == 1) {
                        $(".user_availability_status").css('color', 'red').text('Username already exsists');
                        //return true;
                        u_error = 1;
                    } else {
                        //return true;

                        $(".user_availability_status").css('color', 'green').text('Username Available.');
                        //$('#myform').submit();
                        u_error = 0;
                    }
                },
                error: function () {
                alert('Error while request..');
                }
            });


        }

    });             

    $('#myform').submit(function(e) {
        if(u_error == 1) {
            e.preventDefault();
        }
    });
}(jQuery));

html:

<form id="myform" method="post">
<input type="text" name="username" id="username" required>
<span class="user_availability_status"></span>
<input type="password" name="pas" required>
<input type="submit" name="r_sub" id="mysub">
</form>

更改如下:-

$(".user_availability_status").css('color','red').html('Username already exists');

$(".user_availability_status").css('color','green').html('Username Available.');

$('#myform').submit(function(e) {
    if($(".user_availability_status").html() == 'Username already exists') {
       e.preventDefault();
       return false;
    }
});