将$ .ajax()函数返回的值与字符串进行比较

时间:2011-02-08 08:42:03

标签: jquery ajax

if部分无效

var name = $("#name");

$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        if( msg == "available" )
        {
            // you conditional code here
            // i want to display a image here with id nameInfo
            $('#result').hide();

            $("#result").html(msg).fadeIn("slow");
        }
    }
});

5 个答案:

答案 0 :(得分:1)

$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        if( msg == 'available' )
        {
            // you conditional code here
        }

        $('#result').hide();

        $("#result").html(msg)
        .fadeIn("slow");
    }
});

答案 1 :(得分:0)

  var name = $("#name");

  $.ajax({
    type: "get",
    url: "test.jsp",
    data: "name="+name,
    success: function(msg) {
        if (msg === 'available') {
           $('#result').hide();
           $("#result").html(msg)
                       .fadeIn("slow");
        } else {
           // Do something else here
        }
    }
  });

答案 2 :(得分:0)

$.ajax({
    type:       "get",
    url:        "test.jsp",
    data:           "name="+name,
    success:    function(msg) {

        // msg is returned by the ajax function
        // this holds the returned data

        alert(msg);

        // Compare the msg variable to see if it returned condition
        // this compares the ENTIRE text of return, if it has any more
        // characters in it, this condition will never be met!
        if(msg == "available"){
            alert("Yes!");
        }else{
            alert("No :-(  ");   // Sad face :(  The return was something different
        }

    }
});

答案 3 :(得分:0)

 $.ajax(
         {
          type: "POST",
          url: "test.php",
          data: $('#frmlogin').serialize()
        }
         ).done(
            function( resp ) {
                //alert(resp.indexOf("Email"));
                if(resp.indexOf("Email") > -1){
                    alert("Email text available on response");

                } else {
                    alert("Email text not available on response");   
                }
            }
        );

答案 4 :(得分:-1)

修剪您的回复代码并检查条件

 success: function(msg) {
                        if($.trim(msg) == '1') {
                            console.log("done");
                        } else {
                            console.log("fail");
                        }   

                }