代码之家  ›  专栏  ›  技术社区  ›  Hacker

jquery数组问题

  •  1
  • Hacker  · 技术社区  · 15 年前
    <?php
    include_once('db.php');
    $location = $_POST['location'];
    $doctor = $_POST['doctor'];
    $patient_id = $_POST['patient_id'];
    
    if(($location != "") && ($doctor != "")) {
      $sql = "select Name,Age,Gest_age,Weight from rop_form where Location = '".$location."' and Doctor = '".$doctor."' and Patient_id = '".$patient_id."'";
      $result = mysql_query($sql);
      $myresult  = "";
      while($row = mysql_fetch_array($result)) {
        $myresult1['Patient_id'] = 'R'.$patient_id; 
        $myresult1['Name'] = $row['Name']; 
        $myresult1['Age'] = $row['Age']; 
        $myresult1['Weight'] = $row['Weight']; 
        $myresult1['Gest_age'] = $row['Gest_age']; 
      }
      $myresult = json_encode($myresult1); 
    }
    else {
      $myresult .= "";
    }
    echo $myresult;
    ?>
    

    这是我的PHP代码。

    这是jquery代码。

    $("#patient_id").change(function() {
      $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(json_data) {
        alert(json_data);
        //var my_json = //{"Patient_id":"R00020","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"//};
        $.each(json_data, function(key,value) {
          alert(key + ': ' + value);
          if(key == 'Name'){ $("#name").val(value); }
          if(key == 'Age'){ $("#age").val(value); }
          if(key == 'Weight'){ $("#ropweight").val(value); }
          if(key == 'Gest_age'){ $("#gest_age").val(value); }
        });
      });
    });
    

    alert(json_data);此行正确打印如下

    {"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"} jquery需要哪个foomat

    但是.each循环语句如下: alert(key + ': ' + value); 不是像病人一样打印,而是像病人一样打印。 0:{ 1:P 2:a 3:t 4:i …可能有什么问题?

    4 回复  |  直到 13 年前
        1
  •  2
  •   Andy E    15 年前

    除了Matt Ellen的回答之外, $.each() 方法用于在javascript数组和类似数组的对象(具有length属性)上循环。PHP的关联数组(关键字->值)被转换为本机JavaScript对象。你可以使用 for...in 循环代替:

    for (var key in json_data) { 
       alert(key + ': ' + json_data[key]); 
       if(key == 'Name'){ $("#name").val(json_data[key]);} 
       if(key == 'Age'){ $("#age").val(json_data[key]);} 
       if(key == 'Weight'){ $("#ropweight").val(json_data[key]);} 
       if(key == 'Gest_age'){ $("#gest_age").val(json_data[key]);} 
    }
    

    但你可能不需要这个循环。您只需使用:

    $.post (
      "/diabetes/patient_detail_change.php",
      { 
        location:$("#location").val(),
        doctor:$("#doctor").val(),
        patient_id:$("#patient_id").val()
      }, 
      function (json_data){
        if ("Name" in json_data) { $("#name").val(json_data.Name);}
        if ("Age" in json_data) { $("#age").val(json_data.Age);}
        if ("Weight" in json_data) { $("#ropweight").val(json_data.Weight);}
        if ("Gest_age" in json_data) { $("#gest_age").val(json_data.Gest_age);}
      }, 
      "json"
    );
    
        2
  •  2
  •   Matt Ellen Bipin Vayalu    15 年前

    在您的post语句中,您需要指定您正在返回JSON。

    $.post("/diabetes/patient_detail_change.php",{location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(json_data){
    
    alert(json_data);
    
    $.each(json_data, function(key,value){
    alert(key + ': ' + value);
    if(key == 'Name'){ $("#name").val(value);}
    if(key == 'Age'){ $("#age").val(value);}
    if(key == 'Weight'){ $("#ropweight").val(value);}
    if(key == 'Gest_age'){ $("#gest_age").val(value);}
    
    });
    }, "json");
    

    像这样。

    此时,返回的数据被视为字符串,因此每条语句都输出每个字符。

    请参见此处的定义 jQuery post definition

        3
  •  1
  •   Tinku    15 年前

    你应该使用 $.post ("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()}, function (json_data){ //blah blah },"Json" );

    请检查最后一个参数“json”

        4
  •  1
  •   meouw    15 年前

    使用 $.getJSON 而不是.post美元。
    这将返回一个对象(解析的JSON),而不是字符串