如何比较JavaScript中的值?

时间:2015-08-10 04:28:02

标签: javascript

如何比较JavaScript中的值?

我的代码应提醒" 2"但它不起作用。

                [client] 0s GET /test  
                [server] 1s got request
                /                        \
[server] 2s return response              [server] 2s query db
           |                             [server] 3s db return
           |                                      |  
           |                                      |  
           |                                      |  
[client] 3s get fake response[status]    [client] 4s get real response[data]     

4 个答案:

答案 0 :(得分:2)

您必须使用比较运算符(==,===,!=,!==)。 ==和!=是类型不敏感的,并对第二个操作数执行隐式转换。 ===和!==操作数正在执行类型敏感的比较,包括类型检查。

 var test_val = "TWO";
 if(test_val === "ONE")
 {
     alert("1");
 }
 else if(test_val === "TWO")
 {
     alert("2");
 }

类型敏感

//1 === "1" false
//1 === 1   true
//1 !== "1" true
//1 !== 1   false

输入不敏感

//1 == "1" true
//1 == 1   true
//1 == 2   false
//1 == "2" false

答案 1 :(得分:1)

单个=是赋值(用于设置值)

double ==用于比较,将返回true / false。

三元组===用于比较值和类型。仅当值和类型匹配时才会返回true / false。

答案 2 :(得分:0)

对此最好的答案是,无论何时进行比较,都不能在括号内使用=。您可以使用==或===,具体取决于您要比较的内容。这是你可以做的:

<?php
  mysql_connect("mysql.hostinger.in","username","password");
  $db= mysql_select_db("db");
  $password=$_POST["password"];
  $username=$_POST["username"];

  if (!empty($_POST)) {
  if (empty($_POST['username']) || empty($_POST['password'])) {
  // Create some data that will be the JSON response 
          $response["success"] = 0;
          $response["message"] = "One or both of the fields are empty .";

          //die is used to kill the page, will not let the code below to be executed. It will also
          //display the parameter, that is the json data which our android application will parse to be //shown to the users
          die(json_encode($response));
      }
  $query = " SELECT * FROM test WHERE username = '$username'and password='$password'";

     $sql1=mysql_query($query);
  $row = mysql_fetch_array($sql1);
  if (!empty($row)) {
       $response["success"] = 1;
          $response["message"] = "You have been sucessfully login";
         die(json_encode($response));
  }
  else{

  $response["success"] = 0;
          $response["message"] = "invalid username or password ";
  die(json_encode($response));
  }



  }
  else{

  $response["success"] = 0;
          $response["message"] = " One or both of the fields are empty ";
  die(json_encode($response));
  }

  mysql_close();
  ?>

答案 3 :(得分:0)

= is Assignment运算符。

使用==关系运算符。

参考: http://www.tutorialspoint.com/java/java_basic_operators.htm