如何从json响应中删除html代码

时间:2016-05-27 11:32:42

标签: javascript jquery json

我正在尝试通过表单文本框更改事件中的ajax调用get_child方法。我想在datalist中显示结果。下面是我用过的代码。

    $sql = "SELECT * FROM tbl_child Where `id_mother`=?";
    $results = $db->load_result($sql,array('M-00000001'));
    $child = array();
    foreach($results as $row){  
        $child[]=$row;
    }
    echo json_encode($child,JSON_PRETTY_PRINT);
    die;

我的脚本是:

$('#mother_name').on('keyup', function(e){
     //e.preventDefault();       
    $.ajax({
        url:"<?php echo $this->to_url('get-child'); ?>",
        type:"GET",
        datatype : "json",
        contentType: "application/json; charset=utf-8",
        success: function(data, status){
            console.log(data);
            //$(data).each(function() {
            //  names = "<option value=\"" + this.id_child + "\">" + this.child_name + "</option>";
            //  $('#childname').append(names);
            //});

        },
        error: function(xhr, desc, err){
            console.log(xhr);
        }
    });
});

但是当我打电话时,会显示以下输出。它包含带有结果的html标签。当我从结果中选择特定数据时,它说“未定义”我如何解决这个问题请帮助我。我是json的新手。              

             
  • 菜单
  •          
  • 菜单2
  •       
      

[
{
    "id_child": "0000000001",
    "id_mother": "M-00000001",
    "child_name": "marli",
    "child_lname": "",
    "dob": "2015-05-09 00:00:00",
    "gender": "1",
    "birth_weight": "3100.00",
    "birth_height": "55.00",
    "head_Perimeter": "34.00",
    "reg_by": "O-00000001",
    "created_date": "2016-05-12 21:40:25",
    "10": "2016-05-12 21:40:25"
}]

this is the output

谢谢你们

2 个答案:

答案 0 :(得分:0)

[
{
    "id_child": "0000000001",
    "id_mother": "M-00000001",
    "child_name": "marli",
    "2": "Kathirvelan",
    "child_lname": "",
    "dob": "2015-05-09 00:00:00",
    "gender": "1",
    "birth_weight": "3100.00",
    "birth_height": "55.00",
    "head_Perimeter": "34.00",
    "reg_by": "O-00000001",
    "created_date": "2016-05-12 21:40:25",
    "10": "2016-05-12 21:40:25"
}]

据我所知,这里没有html,而是一个元素数组。

答案 1 :(得分:0)

 data =   [
    {
        "id_child": "0000000001",
        "id_mother": "M-00000001",
        "child_name": "marli",
        "child_lname": "",
        "dob": "2015-05-09 00:00:00",
        "gender": "1",
        "birth_weight": "3100.00",
        "birth_height": "55.00",
        "head_Perimeter": "34.00",
        "reg_by": "O-00000001",
        "created_date": "2016-05-12 21:40:25",
        "10": "2016-05-12 21:40:25"
    }]

Where is the html tag in the above output? and if you want to read any object value

console.log(data[0].id_child);

because its an array object, you have to put index to read the value.