【问题标题】:jQuery autocomplete suggests all options regardless of input entry无论输入项如何,jQuery 自动完成都会建议所有选项
【发布时间】:2016-09-07 03:53:27
【问题描述】:

我有一个 jQuery 脚本,它将获取 JSON 响应并创建响应中尽可能多的“播放器”对象。

然后它将添加到availablePlayers,然后我将其用作autocompletesource: 字段的变量

当用户选择玩家名称并点击“添加”按钮时,此时只会显示玩家的guidname

但是,无论我输入什么字母,所有玩家都可以选择。为了说明这一点,如果我输入“Z”并且没有玩家的名字中有 Z,他们的选项仍然会显示。

如何优化此功能?

HTML

<div class="player-widget">
    <label for "players">Players</label>
    <input id="player" />
    <input id="playerGUID" hidden />
    <button id="add">Add</button>
</div>

jQuery

$(document).ready(function(){

var availablePlayers = []; // BLANK ARRAY OF PLAYERS

$("#player").autocomplete({
    source: availablePlayers,
    response: function (event, ui) {
        ui.content = $.map(ui.content, function(value, key) {
            return {
                label: value.name,
                value: value.guid
            }
        });
    },
    focus: function(event, ui) {
        $("#player").val(ui.item.label);
        return false;
    },
    select: function (event, ui) {
        $("#player").val(ui.item.label); // display the selected text
        $("#playerGUID").val(ui.item.value); // save selected id to hidden input
        return false;
    }
});

$.getJSON("http://localhost/Websites/Player-Widgets/service.php", function(data) {

    var feedHTML = '';
    // LOOP THROUGH EACH PLAYER
    $.each(data.players, function(i, player) {

        // DEFINE VARIABLES - BASED ON PLAYER ATTRIBUTES
        var guid = player.guid;
        var name = player.name;
        var dob = player.date_of_birth;
        var birth = player.birthplace;
        var height = player.height;
        var weight = player.weight;
        var position = player.position;
        var honours = player.honours;

        // CREATE NEW PLAYER (OBJECT)
        var player = {
            guid: guid,
            name: name,
            position: position
        };

        // ADD TO PLAYER TAG ARRAY
        availablePlayers.push(player);
    });

    console.log("User friendly array");
    $.each(availablePlayers, function(i, val) {
        console.log(val.guid + " - " + val.name + " [" + val.position + "]");
    });

    console.log("Array printout");
    console.log(JSON.stringify(availablePlayers));

}).done(function(){
    console.log("Done! Success!");
    $("#player").autocomplete("option", "source", availablePlayers);
});

$("#add").click(function() {
    alert($("#playerGUID").val() + " - " + $("#player").val());
});

});

JSON 响应示例

{
"players": [
    {
        "guid": "1",
        "name": "Matias Aguero",
        "date_of_birth": "1981-02-13",
        "birthplace": "San Nicolas, Argentina",
        "height": "1.83m (6' 0\")",
        "weight": "109kg (17st 2lb)",
        "position": "Prop",
        "honours": "40 caps"
    },
    {
        "guid": "2",
        "name": "George Catchpole",
        "date_of_birth": "1994-02-22",
        "birthplace": "Norwich, England",
        "height": "1.85em (6ft 1\")",
        "weight": "104kg (16st 5lb)",
        "position": "Centre",
        "honours": ""
    }
]
}

【问题讨论】:

  • @RegisPortalez 这个朋友是什么意思?

标签: javascript jquery json autocomplete


【解决方案1】:

您的问题出在源函数中。 源函数使用请求将术语参数传递给查询,而您忽略它。 如果你使用 availablePlayers 来查询,你应该使用

source: availablePlayers

以及您当前的函数以将 {label, text} 对象映射到响应参数中。

response: function (event, ui) {
        ui.content = $.map(ui.content, function(value, key) {
            return {
                label: value.name,
                value: value.guid
            }
        });
    }

【讨论】:

  • 我已根据您的建议更新了上面的代码,但现在根本没有显示任何名称。我的编码是否正确?
  • 是的,是正确的,但是您的 $.get 在源参数中传递它之前没有填充 availablePlayers,请尝试先创建自动完成并在 $.get 成功函数中更新$().autocomplete("option", "source", availablePlayers)
  • 我已经添加了您的建议,首先创建autocomplete,然后更新done() 上的源,但仍然没有成功。我又更新了代码,你能确认一下吗?数组打印输出是 - [{"guid":"1","name":"Matias Aguero","position":"Prop"},{"guid":"2","name":"George Catchpole","position":"Centre"},{"guid":"3","name":"Logovi'i Mulipola","position":"Prop"}]
猜你喜欢
  • 2014-05-04
  • 2012-09-01
  • 2012-10-16
  • 2020-05-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多