试图从json获取关联名称值的id

时间:2014-06-13 11:13:23

标签: php jquery json autocomplete

$(function () {
var availableData = [{
    "Name": "bob barker",
    "image": "./images/bbarker.png"
    "id":"bob"
}, {
    "Name": "Jill bill",
    "image": "./images/jBill.png"
    "id":"jill"
}, {
    "Name": "John Doe",
    "image": "./images/jdoe.png"
    "id":"john"
}]

$('#personsearch').autocomplete({
    source: function (request, response) {
        var re = $.ui.autocomplete.escapeRegex(request.term);
        var matcher = new RegExp("^" + re, "i");
        response($.grep(($.map(availableData, function (v, i) {

            return {
                label: v.Name,
                value: v.Name
            };
        })), function (item) {
            return matcher.test(item.value);
        }))

    }
});
});

    <input type="text" id="personsearch" value="?????" />
    <input type ="submit"/>

我试图在输入字段中获取用于提交目的的ID意味着关联的id带有名称,而id值填入value =“?????”怎么做请帮助

1 个答案:

答案 0 :(得分:0)

您可以使用自动完成的select事件在选择值后填充name属性。

$('#personsearch').autocomplete({
    source: function (request, response) {
        var re = $.ui.autocomplete.escapeRegex(request.term);
        var matcher = new RegExp("^" + re, "i");
        response($.grep(($.map(availableData, function (v, i) {

            return {

                label: v.id,
                value: v.Name

            };
            $("#personsearch").attr("name", value)
        })), function (item) {

            return matcher.test(item.value);
        }))

    },
    select: function (event, ui) {
        $('#personsearch').attr("name", $(this).val());
    }
});

Demo