简体   繁体   English

PHP的JSON数据不工作

[英]php json data not working

I've created an array using php which I've then json encoded 我使用php创建了一个数组,然后对它进行了json编码

$json_array =  json_encode($newarray);

In my jquery, the encoded array seems to be formed OK (i think??) but when I try and use the json data - nothing happens? 在我的jquery中,编码的数组似乎形成正确(我认为?),但是当我尝试使用json数据时-没有任何反应? I'm expecting the textbox to autocomplete. 我期望文本框自动完成。

The funny thing is - it works if I use arraytxt2, but not arraytxt1 (the one created via json_encode). 有趣的是-如果我使用arraytxt2,但不能使用arraytxt1(通过json_encode创建的那个),它可以工作。

Any ideas as to why arraytxt1 is not working?? 关于为何arraytxt1无法正常工作的任何想法? Thanks in advance. 提前致谢。

$(document).ready(function () {
    var arraytxt1 = [{
        "equipmentid": "1",
        "equipmentmake": "Baxi"
    }, {
        "equipmentid": "2",
        "equipmentmake": "Baxi"
    }];

    var arraytxt2 = [{
        "id": "1",
        "label": "aa"
    }, {
        "id": "2",
        "label": "bb"
    }, {
        "id": "3",
        "label": "bbbb"
    }, {
        "id": "4",
        "label": "abab"
    }, {
        "id": "5",
        "label": "cab"
    }];

    $("#txt1").autocomplete({
        source: arraytxt1,
        minLength: 1,
        select: function (event, ui) {
            $("#txt2").val(ui.item.equipmentid);
        }
    });
});

The correct keys for autocomplete's array objects are label and value . 自动完成的数组对象的正确键是labelvalue The value property might not be needed in your case. 在您的情况下,可能不需要value属性。

https://api.jqueryui.com/autocomplete/#option-source https://api.jqueryui.com/autocomplete/#option-source

Try: 尝试:

var arraytxt1 = [{
    "equipmentid": "1",
    "label": "Baxi"
}, {
    "equipmentid": "2",
    "label": "Baxi"
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM