duanji4449 2013-11-14 15:35
浏览 53
已采纳

JSON对象在控制台中显示对象,但也包含php脚本返回的字符串中的'undefined'

I have a php script that queries my database for some values and puts them into a json string.

<?php

if (isset($_POST['name']) === true && empty($_POST['name']) === false) {
    include_once("../php_scripts/connect.php");

    $arr = array();
    if ($result = $mysqli->query("SELECT due_date FROM payment_due_dates WHERE client_id = '" . $_POST['name']. "'")) {
        $rowCount = $result->num_rows;

        echo '[';
        $i = 0;
        while ($row = $result->fetch_assoc()) {
            if ($i <= $rowCount - 2){
                echo json_encode($row);
                echo ',';
            }else{
                echo json_encode($row);
            }
            $i++;
        }
        $result->free();
    }
    echo ']';   
}

?>

Then jquery converts into a json object and adds the values to a select box.

$(document).ready(function(){
          $('#subName').click(function(){

                var name = $('input#txtName').val();

                if($.trim(name) != ''){
                    $.post('ajax/load_patient_episodes.php', {name: name}, function(data){
                    console.log(data);
                    var obj = $.parseJSON(data);
                    console.log(obj);
                        var output;
                        $.each(obj, function(key, val){
                            output += '<option>' + val.due_date + '</option>';
                        });
                        $('#selEpisode2').html("<option>" + output + "</option>");
                    });
                }
          });
        });

It loads the values but also gives this error in my console log:

event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5

and, when it adds the values to the selectbox it also adds the text 'undefined'. So the options in my select box appear as:

undefined

2013-11-13

2013-11-13

2013-11-13

I'm assuming it may have something to do with the proto array that is in there but I'm not sure how it got there. Still somewhat a newbie on this stuff. Here is my console log. Any ideas how I can lose the 'undefined in my select options?' Also, is there a better way I should be going about loading select options from a php query?

event.returnValue is deprecated. Please use the standard event.preventDefault()
instead. jquery.js:5
[Object, Object, Object]
0: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
  • 写回答

3条回答 默认 最新

  • dongpu1881 2013-11-14 15:44
    关注

    Try this

    var output = '';
    $.each(obj, function(key, val){
      if (typeof (val.due_date) != undefined) { 
        output += '<option>' + val.due_date + '</option>';
      }
    });
    

    You might also want to SELECT UNIQUE due_date.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?