我试图以自动完成jQuery方法的形式获取值,并将选定的输入值存储到文本框中(推到文本框)。听起来很简单,但是这个JSON模式有点耗时。

我可以在这里获得快速帮助吗?

http://jsfiddle.net/ebPCq/1/

jQuery代码:

    $("#material_number").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://ws.geonames.org/searchJSON",
            dataType: "json",
            data: {
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function (data) {
                response($.map(data.geonames, function (item) {
                    return {
                        // following property gets displayed in drop down
                        label: item.name + ", " + item.countryName,
                        // following property gets entered in the textbox
                        value: item.name,
                        // following property is added for our own use
                        my_description: item.fcodeName
                    }
                }));
            }
        });

最佳答案

在修复并完成了初始功能的确定之后,我得出了这个小提琴作为我上面发布的查询的解决方案的结论。

http://jsfiddle.net/ebPCq/7/

JS代码:

$(function () {
$("#input").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://ws.geonames.org/searchJSON",
            dataType: "json",
            data: {
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function (data) {
                response($.map(data.geonames, function (item) {
                    return {
                        // following property gets displayed in drop down
                        label: item.name + ", " + item.countryName,
                        // following property gets entered in the textbox
                        value: item.name,
                        // following property is added for our own use
                        my_description: item.fcodeName
                    }
                }));
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {
        if (ui.item) {
            $("#push-input").prepend(ui.item.value + '\r\n');
        }
    }
  });
});

关于javascript - 如何将从JSON API获取的自动完成数据推送到文本框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15972388/

10-11 17:58