如何将包含键值对的json字符串绑定到自动完成jquery文本框?

时间:2014-08-18 17:59:47

标签: javascript jquery jquery-ui autocomplete jquery-ui-autocomplete

我收到了

形式的json字符串
[{"Name":"Hassi","Email":"sx_sem@hotmail.com"},  
{"Name":"Hass","Email":"sx_sem@hotmail.com"},
{"Name":"Sam","Email":"xxx_sem@hotmail.com"}]

我正在关注jquery-ui http://jqueryui.com/autocomplete/#multiple

中给出的示例

我正在为我的电子邮件应用程序执行multiple values autocomplete。但我希望以用户类型名称显示格式,自动填充功能会显示name+emailson selection,它应该只显示email。但我这样做是不吉利的。

这是我的代码:

  function split(val) {
       return val.split(/,\s*/);
  }
  function extractLast(term) {
        return split(term).pop();
  }

     $("#<%=txtEmail.ClientID%>")
       // don't navigate away from the field on tab when selecting an item
       .bind("keydown", function (event) {
       if (event.keyCode === $.ui.keyCode.TAB &&
          $(this).autocomplete("instance").menu.active) {
            event.preventDefault();
       }
     })
     .autocomplete({
         minLength: 0,
         source: function (request, response) {
           // delegate back to autocomplete, but extract the last term
           response($.ui.autocomplete.filter(
           jsonData, extractLast(request.term)));
           // When i am jsonData the whole json string is displaying,
           //I only want to show concatenate name + email
         },
         focus: function () {
           // prevent value inserted on focus
           return false;
         },
         select: function (event, ui) {
           var terms = split(this.value);
           // remove the current input
           terms.pop();
           // add the selected item
           terms.push(ui.item.email);
           // add placeholder to get the comma-and-space at the end
           terms.push("");
           this.value = terms.join(", ");
           return false;
          }
         });

0 个答案:

没有答案