jQuery AJAX not returning false

jQuery AJAX not returning false

本文关键字:false returning not AJAX jQuery      更新时间:2023-09-26

如果员工ID存在,我希望下面的程序返回false。如果雇员ID存在,则PHP文件返回true,并将其返回给AJAX函数。

$.post("connect_ajax_php.php",
    {type: "checkId", val: val, field: "emp_id", table: "employee"})
    .done(function(data, succ){
        data = $.trim(data);
        if( succ =="success" && data=="true" ){
            $( errContId ).html( val+" already exist" );
            $( id ).css( {"border":"1px solid red"} );
            $('#'+sucImg).html("<img src='images/background/error.png'>");
            return false;
        }else{
            $( errContId ).html("");
            $( id ).css( {"border":"1px solid #ccc"} );
            $('#'+sucImg).html("<img src='images/background/success.png'>");
        }
    });

如果您使用ajax调用作为验证步骤,您将在ajax回调中手动提交表单。然后将return false移动到click处理程序,而不是从ajax响应处理程序调用它。

<form id="myform" action="/url" method="post">
    ...
    <button id="submitbtn" type="submit">Submit</button>
</form>
$("#submitbtn").on("click", function(event) {
    $.ajax({
        url: "connect_ajax_php.php",
        method: "post",
        data: {type: "checkId", val: val, field: "emp_id", table: "employee"}
    })
    .done(function(result) {
        if (result == "true") {
            // id exists
        }
        else {
            $("#myform").submit();
        }
    });
    return false;  // prevent standard form submission
});