1

オートコンプリート フォームにこのプラグインを使用しています: http://www.planbox.com/blog/news/updates/jquery-autocomplete-plugin-for-backbone-js.html

以下のコード ( if (inputVal == 'bakaxel')) のように 1 つのアイテムのみをチェックする代わりに、選択した値をコレクション全体に対してチェックしたいと考えています。

var collection = new Backbone.Collection([
    {id:"AB", name:"Alberta"},
    {id:"AD", name:"Album"},
    {id:"BA", name:"barn"},
    {id:"BC", name:"bak"},
    {id:"BD", name:"baby"},
    {id:"BE", name:"band"},
    {id:"BF", name:"bakaxel"},
    {id:"BG", name:"batteri"},
    {id:"BH", name:"barbie"},
    {id:"MB", name:"Manitoba"},
    {id:"AP", name:"Armed Forces Pacific"}
]);

$('input.search').autocomplete({
    collection: collection,
    attr: 'name',
    noCase: true,
    ul_class: 'search_options tr_list',
    ul_css: {'z-index':1234}
});

$('input.search').each(function(){
    $(this).blur(function(){
        var inputVal = $('input.search').val();

        if (inputVal == 'bakaxel') {
            $('#search_result_page').load('searchResult.html');
            $('#searchPage').addClass('hidden');
        }      

    });
});

私はこれを試しましたが、ar 配列を再度作成するのではなく、バックボーン コレクションを使用するだけです。

$('input.search').each(function(){

    $(this).blur(function(){
        var inputVal = $('input.search').val();
        var ar = ["Alberta", "Album", "barn", "bak", "baby", "band", "bakaxel", "batteri", "barbie", "Manitoba", "Armed Forces Pacific"];

        if (jQuery.inArray(inputVal, ar) != -1) {
            $('#search_result_page').load('searchResult.html');
            $('#searchPage').addClass('hidden');
        } 

    });
});
4

1 に答える 1

1

バックボーン プロキシ アンダースコア関数、特にあなたの場合はhttp://underscorejs.org/#where

where _.where(list, properties) リスト
内の各値を調べて、プロパティにリストされているすべてのキーと値のペアを含むすべての値の配列を返します。

あなたのテストは次のように書くことができます

var matches = collection.where({
    name: inputVal
});
if (matches.length>0) {
...
}

または、@mu がコメントで提案したように、http: //underscorejs.org/#findで入力の存在を確認するだけです。

var found = collection.find(function(model) {
    return model.get('name') === inputVal
});
if (found) {
    ...
}
于 2013-02-05T14:23:41.253 に答える