簡體   English   中英

jQuery UI自動完成選擇從JSON獲取所有值

[英]jQuery UI autocomplete select get all values from json

JSON具有鍵:id,n和d。 我需要獲取所選項目的“ id”和“ n”以創建一個網址。

  $.ajax({
    type: "GET",
    url: "q.json",
    success: function(data) {
      var items = [];
      $.each(data, function(key, val) {
        items.push(val);
      });

      $("#tags").autocomplete({ 
        maxResults: 4,
        minLength: 2,
        source: function(request, response) {
          var filteredArray = $.map(items, function(item) {
            if (item.n.toUpperCase().indexOf(request.term.toUpperCase()) == 0) {
              return item.n + " ("+item.d+")";
            } else {
              return null;
            }
          });
          response(filteredArray.slice(0, this.options.maxResults));
        },
        select: function(event, ui) {
          alert("you selected " + ui.item.id);
          window.location.href = ui.item.id+"-"+ui.item.n;
        }
      });

    }
  });

如何達到預期的效果?

我在JSON中有3個鍵,必須在select中獲取它們:function(event,ui)

      $("#tags").autocomplete({ 
          maxResults: 4,
          minLength: 2,
          source: function(request, response) {
            var filteredArray = $.map(items, function(item) {
              if (item.n.toUpperCase().indexOf(request.term.toUpperCase()) == 0) {
                return {
                  id: item.id
                  label: item.n + " ("+item.d+")",
                }
              } else {
                return null;
              }
            });
            response(filteredArray.slice(0, this.options.maxResults));
          },
          select: function(event, ui) {
            event.preventDefault();
            $("#tags").val(ui.item.label);
            window.location.href = ui.item.id+"-"+ui.item.n;
          },
        });

      });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM