ajax成功函数中的条件语句

时间:2012-01-30 11:41:01

标签: jquery ajax codeigniter

我正在使用Codeigniter MVC框架,我正在与Jquery Ajax斗争。 我的问题是你如何将条件语句放在ajax成功函数中?

基本上我要做的是成功输入后会重定向到另一个页面,如果不会显示错误:手机号码不匹配或者是否为空请填空。

控制器方法

if($mobile_number == "") {
                    $data = array('message' => "Please fill up the blanks", 'success' => 'no');

            }
            elseif($mobile_number != "123") {
                    $data = array('message' => "Please does not match", 'success' => 'no');

            }else {
                    #redirect('home/test');
                    $data = array('message' => "Mobile match.", 'success' => 'yes');
            }
            $output = json_encode($data);
            echo $output;

my_ajax.js

$(document).ready(function(){


$("#request_submit").click( 

function(){

    var mobtel=$("#mobtel").val();

    $.ajax({
    type: "POST",
    url: "post_action",
    dataType: "json",
    data: "mob_tel="+mobtel, 
    cache:false,
    success: function (data) {

            if(data.success == 'yes') {
                    $("#form_message").html(data.message).fadeIn('slow'); 
            }
            else if(data.success == 'no') {
                    alert('error');
            }
    }

    });
});
});

提前致谢。希望有人可以帮助我。 (T_T)

3 个答案:

答案 0 :(得分:3)

不确定我是否理解你的问题......从我得到的结果来看,这应该是正确的方向。

如果我是你,我会将我的回复改为以下格式:

$data = array(
 'message'=> /* Your message */,
 'success' => /* True or false */, 
 'case'=> /* 1,2,3 something unique to tell the cases apart */,);

echo json_encode($data);

所以,在jQuery中我可以简单地这样做:

 $.ajax({
   type: "POST",
   url: "post_action",
   dataType: "json",
   data: "mob_tel="+mobtel, 
   cache:false,
   success: function (data) {

        switch(data.case){
        1: /*First case */
        break;
        2: /*Second... */
        break;
        3: /* You know the drill... */
        break;
        default:
        /* If none of the above */

        }
}

});

由于您的代码中有三种情况,因此最好将它们作为三种情况处理。代码更加可靠,不易出错。

我希望这有帮助!

答案 1 :(得分:0)

在Controller方法中:

在功能结束时使用exit();

在my_ajax.js

$(document).ready(function(){


$("#request_submit").click( 

function(){

    var mobtel=$("#mobtel").val();

    $.ajax({
    type: "POST",
    url: "post_action",
    dataType: "json",
    data: "mob_tel="+mobtel, 
    cache:false,
    success: function (data) {

            if(data.success == 'yes') {
                    $("#form_message").html(data.message).fadeIn('slow'); 
            }
            else{

                  if(data.message == "Please does not match"){ 
                    //show error message where ever you want
                      $("#form_message").html(data.message).fadeIn('slow'); 
                  }else{
                    //Do whatever you want to do :: to redirect can use window.location
                       alert('data.message')
                   }

            }
    }

    });
  });
});

答案 2 :(得分:0)

您可以使用for循环来搜索回调对象中的任何键

如果您想查看回调中是否存在错误密钥,您可以使用:

success: function(cb)
{
    for(errors in cb)
    {
       //checking for error key in the callback
    }
}