2015-06-04 50 views
0

我正在創建一個過濾輸入,其中根據keyUp提供結果。這是我在做什麼過濾,通過骨幹的集合:按子串過濾錯誤結果

var Brand = Backbone.Model; 

var Brands = Backbone.Collection.extend({ 
    model: Brand, 
    filterByName: function() { 
     return this.filter(function (model) { 
      return model.get('name').indexOf('h') > -1; 
     }); 
    } 
}); 

var fiat = new Brand ({ name: 'Fiat' }); 
var honda = new Brand ({ name: 'Honda' }); 
var chevrolet = new Brand ({ name: 'Chevrolet' }); 
var peugeot = new Brand ({ name: 'Peugeot' }); 
var mitsubishi = new Brand ({ name: 'Mitsubishi' }); 
var hyundai = new Brand ({ name: 'Hyundai' }); 

var brands = new Brands ([ fiat, honda, chevrolet, peugeot, mitsubishi, hyundai ]); 

console.log(brands.filterByName()); 

遊樂場:http://jsfiddle.net/Lgcb0skm/

的一點是:當我輸入h,例如,它帶給我的只有Mitsubis h i and C h evrolet,而不是所有可能的結果,如H ondaH yundai等等爲什麼?建議?

+1

'獲得( '名稱')toLowerCase()的indexOf( 'H')> -1' – andlrc

+0

請。把你的評論變成接受的答案。我太笨,哈哈。 –

回答

1

簡答'H' != 'h'。您將需要降低的情況下你的文本字符串,如果你想要做的不區分大小寫的匹配:

return this.filter(function (model) { 
    return model.get('name').toLowerCase().indexOf('h') > -1; 
}); 
1

嘗試使用這樣的:

return model.get('name').toLowerCase().indexOf('h') > -1;