2016-05-10 87 views
0

我用作基礎this問題哪個起作用。當使用複雜數組時,jquery自動完成功能不起作用

我創建了一個JsFiddle,試圖找出爲什麼這是行不通的,對我的生活,我不能工作了。即使輸入「H」的結果作爲lastName的第一個字母,它也不會找到任何結果。

有人可以看看,看看這是爲什麼不拉的比賽?

 $(document).ready(function asdf() { 


var users = [ 
{ 
    id: "5", 
    userName: "Tclyde", 
    firstName: "Terry", 
    lasttName: "Adams", 
}, 
{ 
    id: "6", 
    userName: "LH", 
    firstName: "Leonie", 
    lasttName: "Henderson", 
}, 
{ 
    id: "7", 
    userName: "CharlesO", 
    firstName: "Charles", 
    lasttName: "O'Dwyer", 
}, 
{ 
    id: "2", 
    userName: "si2030", 
    firstName: "Simon", 
    lasttName: "O'Farrell", 
}, 
{ 
    id: "4", 
    userName: "blade44", 
    firstName: "Clyde", 
    lasttName: "Palmer", 
}, 
{ 
    id: "3", 
    userName: "tt2030", 
    firstName: "David", 
    lasttName: "Remmy", 
}]; 

$("#search").autocomplete({ 
       source: function (req, responseFn) { 
        var re = $.ui.autocomplete.escapeRegex(req.term); 
        var matcher = new RegExp("^" + re, "i"); 
        var a = $.grep(users, function (item, index) { 
         return matcher.test(item.lastName); 
        }); 
        a = $.map(a, function (x) { 
         return { 
          label: x.lastName + ", " + x.firstName, 
          value: x.id, 
          users: x 
         }; 
        }); 
        responseFn(a); 
       }, 
       select: function (event, ui) { 
        location.href = "/UserAdmin/Edit/" + ui.item.id; 
       }, 
       change: function (event, ui) { 
        if (!ui.item) { 
         $("#search").val(""); 
        } 
       } 
      }); 
}); 

回答

1

有幾個錯別字。有一個關鍵lasttName,但使用的是作爲lastName

$(document).ready(function() { 
     var users = [{ 
     id: "5", 
     userName: "Tclyde", 
     firstName: "Terry", 
     lasttName: "Adams", 
     }, { 
     id: "6", 
     userName: "LH", 
     firstName: "Leonie", 
     lasttName: "Henderson", 
     }, { 
     id: "7", 
     userName: "CharlesO", 
     firstName: "Charles", 
     lasttName: "O'Dwyer", 
     }, { 
     id: "2", 
     userName: "si2030", 
     firstName: "Simon", 
     lasttName: "O'Farrell", 
     }, { 
     id: "4", 
     userName: "blade44", 
     firstName: "Clyde", 
     lasttName: "Palmer", 
     }, { 
     id: "3", 
     userName: "tt2030", 
     firstName: "David", 
     lasttName: "Remmy", 
     }]; 

     $("#search").autocomplete({ 
     source: function(req, responseFn) { 
      addMessage("search on: '" + req.term + "'<br/>"); 
      var re = $.ui.autocomplete.escapeRegex(req.term); 
      var matcher = new RegExp("^" + re, "i"); 
      var a = $.grep(users, function(item, index) { 
      return matcher.test(item.lasttName); //Changed here 
      }); 
      a = $.map(a, function(x) { 
      return { 
       label: x.lasttName + ", " + x.firstName, //Changed here 
       value: x.id, 
       users: x 
      }; 
      }); 
      addMessage("Result: " + a.length + " items<br/>"); 
      responseFn(a); 
     }, 
     select: function(event, ui) { 
      location.href = "/UserAdmin/Edit/" + ui.item.id; 
     }, 
     change: function(event, ui) { 
      if (!ui.item) { 
      $("#search").val(""); 
      } 
     } 
     }); 

     function addMessage(msg) { 
     $('#msgs').append(msg); 
     }; 
}); 

入住這jsFiddle

+0

超..這工作。向上投票並正確答案。 – si2030

相關問題