jQuery的'如果'语句字符串比较不工作 [英] jQuery 'If' statement string comparison not working

查看:118
本文介绍了jQuery的'如果'语句字符串比较不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常困难的时间试图做一些很简单的。这里的code:

 如果(数据=='成功'){

            警报('富');

        } 其他 {

            警报(数据);

        }
 

我已经简化了,但是这一切必须了解发生了什么事情。变量'数据'是一个AJAX调用的结果,如果有什么差别。问题是,它总是转到别人的发言,并提醒成​​功,它不应该如果去到'其他'。任何想法是怎么回事?

编辑:下面是完整的AJAX code jQuery的:

  $岗位(/ manage_sites.php,{前:前编辑:后},功能(数据){

        如果(数据==成功){

            警报('嗒嗒');

        } 其他 {
            警报(数据);
        }
    });
 

然后在PHP的响应:

  ... code ....
$更新=请求mysql_query(更新用户SET饲料='$ afterFeed其中username ='$名字')或死亡(查询失败);

如果($更新){

    回声成功; //这是字符串,正在给数据
}
 

解决方案

您可以在客户端使用修复 $修剪() 这样的:

 如果($。修剪(数据)=='成功'){
 

或者,更好的办法是去除新线从服务器端来了,可能是一个错误的新线在你的PHP的地方,之前或之后的&LT ;?检查?> 块,这是最常见的,他们突然出现

或者输出你的内容,像这样经过短短退出:

 如果($更新){
  回声成功;
  出口();
}
 

I'm having a very hard time trying to do something very simple. Here's the code:

        if(data == 'success') {

            alert('foo');

        } else {

            alert(data);

        }

I've simplified it, but that's all that's necessary to understand what's going on. the variable 'data' is a result of an AJAX call, if that makes any difference. The problem is that it always goes to the 'else' statement and it alerts 'success', which it shouldn't if it goes to the 'else'. Any idea what's going on here?

EDIT: Here's the full AJAX code in jQuery:

$.post("/manage_sites.php", {before:before, edit:after}, function(data){

        if(data == success) {

            alert('blah');

        } else {
            alert(data);
        }
    });

And then in the PHP response:

...code....
$update = mysql_query("UPDATE users SET feeds = '$afterFeed' WHERE username = '$name'") or     die("Query Failed");

if($update) {

    echo  'success';  //this is the 'string' that is being given to 'data'
}

解决方案

You can fix it on the client side using $.trim() like this:

if($.trim(data) == 'success') {

Or, a better approach would be removing the new-line coming from the server-side, probably an erroneous new-line in your PHP somewhere, check before or after the <? ?> block, this is most often where they crop up.

Or, just exit after outputting your content, like this:

if($update) {
  echo 'success';
  exit();
}

这篇关于jQuery的'如果'语句字符串比较不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆