如何在单独的文本框中从自动完成响应中获取json密钥和值。请参阅图片

时间:2016-08-18 07:38:33

标签: jquery html jsp servlets

enter image description here

请参阅您将了解的图片...此图片取自Chrome开发工具,我在控制台上打印JSON响应。

以下是自动填充代码..

$("#search-org").autocomplete({
    width: 300,
    max: 10,
    delay: 100,
    minLength: 1,
    autoFocus: true,
    cacheLength: 1,
    scroll: true,
    highlight: false,
    source: function(request, response) {
        $.ajax({
            url : "../../SearchHelperController",
            contentType : "application/json; charset=utf-8",
            dataType : 'json',
            type : 'GET',
            cache : false,
            data: {
                searchKeyword: request.term,
                searchType: $("#byName").val()
            },
            success: function(data, textStatus, jqXHR) {
                console.log(data);
                response(data);
            },
            error: function(jqXHR, textStatus, errorThrown){
                 console.log( textStatus);
            }

        });
    }, 
    select: function (event, ui) {
        $("#org-id").val(ui.item.key);
    },

});

html代码..

<div class="radio">
            <label><input type="radio" id="byName" name="byName" value="OU" checked> By Name</label>
            <input type="text" id="org-id">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" id="search-org">
        </div>

2 个答案:

答案 0 :(得分:1)

@AhmadBamieh,感谢您的支持 我解决了这个问题,这是代码,

Html代码

<form action="#">
    <table align="center" style="padding-top: 5%">
        <tr>
            <td align="right">AutoComplete:</td>
            <td><input id="autoText" name="autoText" autofocus/></td>

            <td align="right">Key:</td>
            <td><input id="key" /></td>
        </tr>
    </table>
</form>

JQuery自动填充代码

$(document).ready(function() {          
        var value;          
        $("#autoText").autocomplete({
            width: 300,
            max: 10,
            delay: 100,
            minLength: 1,
            autoFocus: true,
            cacheLength: 1,
            scroll: true,
            highlight: false,
            source: function(request, response) {
                $.ajax({
                    url : "../AutoCompleteController",
                    contentType : "application/json; charset=utf-8",
                    dataType : 'json',
                    type : 'get',
                    cache : false,
                    data: {
                        term : request.term,
                    },
                    success: function(data) {
                        value = data;
                        response(data);
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                         console.log( textStatus);
                    }

                });
            }, 
            select: function (event, ui) {
                lable = ui.item.value;
                $.each(value, function(key, value){
                    if(lable == value) {
                        $("#key").val(key);
                    }
                });
            },

        });
    });

答案 1 :(得分:0)

获取对象键,只需写:

var keys = Object.keys(data);

这将为您提供此对象的键数组。访问它们使用

keys.forEach(function(key) {
   console.log(data[key]);
});