繁体   English   中英

Codeigniter表单验证返回问题

[英]codeigniter form validation return problems

我已经对我的注册进行了一些表格验证。 Sofar我可以在我的$ ajax成功函数中得到对还是错,但是不能将其返回到剩余的代码中……有人可以为此解决方案吗?

function checkIfIsInDB(o, n){
    var target_url = window.location.pathname + 'user/check_if_username_exist',
    valid = '';        
    $.ajax({
        type: "POST",
        dataType: "json",
        url: target_url,
        data: {
            username: o.val()
        },
        success: 
        function(json){
            if(json['check']===false){
                o.addClass( "ui-state-error" ); 
                updateTips( n + " has been alredy taken." );
            }
            if(json['check']===true){
                valid = true;
            }  
        }

    });
    alert(valid); //return(valid);   need for returning true or false
}

您正在异步工作,因此需要在$ .ajax()函数的成功部分中添加“处理代码”:callback部分。

您已通过失败的检查正确完成了此操作,只需在

if(json['check'] ===true){
    //put your success code here - call another function perhaps
}

部分。

尝试在成功回调中移动alert()

function checkIfIsInDB(o, n) {
    var target_url = window.location.pathname + 'user/check_if_username_exist',
        valid = '';
    $.ajax({
        type: "POST",
        dataType: "json",
        url: target_url,
        data: {
            username: o.val()
        },
        success: function (json) {
            if (json['check'] === false) {
                o.addClass("ui-state-error");
                updateTips(n + " has been alredy taken.");
            }
            if (json['check'] === true) {
                valid = true;
            }
            alert(valid); //return(valid);   need for returning true or false
        }

    });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM