简体   繁体   中英

php json data not working

I've created an array using php which I've then json encoded

$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? 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).

Any ideas as to why arraytxt1 is not working?? 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 . The value property might not be needed in your case.

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

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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