【问题标题】:Selected item should not shown in auto complete list所选项目不应显示在自动完成列表中
【发布时间】:2015-12-18 04:43:19
【问题描述】:

我正在使用自动完成 Web 服务 sing JSON,如果我选择的列表项不能再次出现在自动完成列表中;

JSON AJAX 代码:

 select: function (event, ui) {
                    var terms = split(this.value);
                    if (terms.length <= 10) {
                        // remove the current input
                        terms.pop();
                        // add the selected item
                        terms.push(ui.item.value);
                        // add placeholder to get the comma-and-space at the end
                        terms.push("");
                        this.value = terms.join(", ");
                        return false;
                    }
                    else {
                        var last = terms.pop();
                        $(this).val(this.value.substr(0, this.value.length - last.length - 0)); // removes text from input
                        $(this).effect("highlight", {}, 1000);
                        $(this).addClass("red");
                        $("#warnings").html("<span style='color:red;'>Max skill reached</span>");
                        return false;
                    }
                }

我也附上截图,请看这里:

【问题讨论】:

  • 你能提供一个jsfiddle,或者至少更多的代码(html等)
  • 介意我问为什么?如果用户删除它,它是否必须回到列表中?
  • @Bindrid,我不知道你在问什么,你的问题对我来说是有线的。对不起
  • @Fraser,这里的fiddlr代码:fiddlr Link
  • 从选项列表中删除所选项目并不常见,所以我想知道您为什么会这样。顺便说一句,有一个名为 select2 插件的插件可以完成很多我认为您正在尝试做的事情。

标签: json web-services jquery-ui asp.net-ajax jquery-autocomplete


【解决方案1】:

就像你的问题在 cmets 中提到的 @Bindred 一样,更简单的解决方案是使用 Select2 jQuery 库。这不是您正在寻找的东西,但就 UX 而言,我认为它会实现类似的目标,并且可以轻而易举地开始工作。

我添加了一个示例供您使用:https://jsfiddle.net/9cqc5876/9/

HTML

<select id="txtExpertise" multiple="multiple"></select>

JavaSript

$(document).ready(function() {

   $("#txtExpertise").prop("disabled", "disabled");

  // do your ajax request for data
  //$.getJSON("../WebServices/WebServiceSkills.asmx/GetAutoCompleteData", function(data) {

    // fake json data
    var data = {"languages": ["Java", "C", "C++", "PHP", "Visual Basic", 
      "Python", "C#", "JavaScript", "Perl", "Ruby"]};

    // populate the select
    $.each(data.languages, function(key, val) {
      $('#txtExpertise')
        .append($("<option></option>")
          .attr("value", key)
          .text(val));
    });

    // activate the select2
    $("#txtExpertise").select2();
    $("#txtExpertise").prop("disabled", false);

  //});
});

【讨论】:

  • 谢谢@Fraser 来帮助我。你能为我提供标记化的 Javascript 版本链接吗?我也想像标签一样自动完成
  • @Aman jsfiddle 中的示例已经有了,不是吗?查看选择 2 个示例页面。它有很多选项和代码示例。
  • 我知道,但是,我想知道,什么 javascript 和 jquery 库文件版本适合标记。
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
相关资源
最近更新 更多