1

如果数据为空,我想返回 true,如果数据不为空,我想返回 false。如果数据为空,则继续执行表单操作,否则留在当前页面。

代码是:

<script type="text/javascript">
    function error()
    { 
        var response = false;
        $.ajax({
            url: BASE_URL + 'auth/validateForm',
            type: 'POST',
            data: {
                surname: $("#r-surname").val()
            },
            dataType: "json",
            success: function(data) { 
                if(data.surname)
                { 
                    $('#error_surname').html(data.surname);
                    response = false;
                }else{
                    $('#error_surname').html('');
                    response = true;
                }

            },
            error: function(data) { 
                        return false;
            }
        });
        alert(response);
            return response;
    }

</script>

<form action="{$BASE_URL}contul-meu" method="post" name="registration" onsubmit="return error()">
                        <table>                              
                            <tr>
                                <td>
                                    <label for="r-surname">Prenume*</label>
                                </td>
                                <td>
                                    <input type="text" class="text" id="r-surname" name="surname"  value="{$user['surname']}"/>
                                </td>
                                <td><small id="error_surname" class="err"></small></td>
                            </tr>
                        </table>
 </form>

php:

public function validateForm()
{
    $surname = $this->input->post('surname');
    $data = array();
    if(strip_tags($surname) == '')
    {
        $data['surname'] = 'Prenumele este invalid!';
    }

    echo json_encode($data);
}

var response 只是 false。如果没有错误,如何使响应为 true?

4

3 回答 3

2

您不能返回嵌套 ajax 函数的结果。javascript 将执行并继续运行 - 但是在函数返回后会延迟发出请求。

最好的办法是链接你的事件。只需创建另一个接受 true 或 false 值的函数,并将您的逻辑包装在其中。

<script type="text/javascript">
function error()
{ 
    var response = false;
    $.ajax({
        url: BASE_URL + 'auth/validateForm',
        type: 'POST',
        data: {
            surname: $("#r-surname").val()
        },
        dataType: "json",
        success: function(data) { 
            if(data.surname)
            { 
                $('#error_surname').html(data.surname);

                response = false;
                doSomethingBasedOnResponse(false);
            }else{
                $('#error_surname').html('');
                response = true;
                doSomethingBasedOnResponse(true);
            }

        },
        error: function(data) { 
                    return false;
            doSomethingBasedOnResponse(false);
        }
    });

}

function doSomethingBasedOnResponse(value){
    if(value){
        alert('yes');
    }else{
        alert('no');
    }
}

</script>
于 2013-10-13T12:51:11.817 回答
0

在表单中添加 ID

<form id="registration"..

$("#registration").submit(function(e){
    $.ajax({
        url: BASE_URL + 'auth/validateForm',
        type: 'POST',
        data: {
            surname: $("#r-surname").val()
        },
        dataType: "json",
        success: function(data) { 
            if(data.surname)
            { 
                $('#error_surname').html(data.surname);
                //prevent form submission
                e.preventDefault();
            }else{
                $('#error_surname').html('');
                //no errors, it will continue with form submission
            }
        },
        error: function(data) { 
                    e.preventDefault();
        }
    });
});
于 2013-10-13T12:41:34.843 回答
-1

实际检查返回是否为空或 null。

success: function(data) { 
                if((data != null || data != "") && data.hasOwnProperty("surname"))
                { 
                    $('#error_surname').html(data.surname);
                    response = false;
                }else{
                    $('#error_surname').html('');
                    response = true;
                }
于 2013-10-13T12:41:15.150 回答