JavaScript自动完成插件 - 如何排除对象中的参数?

时间:2015-09-18 15:52:34

标签: javascript jquery object autocomplete javascript-objects

我使用devbridge.com名为自动填充的插件。我遇到问题的是过滤自动填充结果以排除某些选项,具体取决于用户在其他文本框中输入的内容。这是我的HTML:

<div class="check_in_questions">
    <label id="label_new_home" for="location_pickup">Pickup Up Location</label>
    <br>
    <input id="location_pickup" class="autocomplete" type="text" placeholder="Pickup Location">
</div>
<div class="check_in_questions">
    <label id="label_new_home" for="location_dropoff">Drop-Off Location</label>
    <br>
    <input id="location_dropoff" class="autocomplete" type="text" placeholder="Drop-Off Location">
</div>

和JavaScript:

var data = [
{ 
    value: 'Orlando International Airport (MCO)', 
    data: { 
            category: 'Airport',
            address: '1 Jeff Fuqua Blvd., Orlando, FL',
            airport: 'MCO',
            location: 'Orlando'
          } 
},
{ 
    value: 'Orlando Sanford International Airport (SFB)', 
    data: { 
            category: 'Airport',
            address: '1200 Red Cleveland Blvd., Sanford, FL',
            airport: 'SFB',
            location: 'Orlando'
          } 
},
{ 
    value: 'Port Canaveral Cruise Terminal', 
    data: { 
            category: 'Cruise Terminal',
            address: 'Port Canaveral, FL',
            airport: '',
            location: 'Port Canaveral'
          } 
},
{ 
    value: 'Baymont Inn & Suites Florida Mall/Orlando', 
    data: { 
            category: 'Hotel',
            address: '8820 S Orange Blossom Trail, Orlando, FL',
            airport: '',
            location: 'Orlando'
          } 
}
];


$('.autocomplete').devbridgeAutocomplete( {
    lookup: data,
    groupBy: 'category',
    lookupFilter: function (suggestion, query, queryLowerCase) {
        return suggestion.value.toLowerCase().indexOf(queryLowerCase) === 0 
        || suggestion.data.category.toLowerCase().indexOf(queryLowerCase) === 0 
        || suggestion.data.location.toLowerCase().indexOf(queryLowerCase) === 0 
        || suggestion.data.address.toLowerCase().indexOf(queryLowerCase) === 0 
        || suggestion.data.airport.toLowerCase().indexOf(queryLowerCase) === 0;
    }
});

$('#location_pickup').change(function() {
    if (get_location($(this).val()) != '') {
        $(this).setOptions({ params: 
                            { data: 
                            { location: !get_location($('#location_dropoff').val()) }
                            } 
                          });
    }
});

$('#location_dropoff').change(function() {
    if (get_location($(this).val()) != '') {
        $(this).setOptions({ params: 
                            { data: 
                            { location: !get_location($('#location_pickup').val()) }
                            } 
                          });
    }
});

function get_location(string) {
    var location;

    if (data.some(function(item, index, array) { i = index; return item.value == string})) {
        location = data[i].data.location;
    }

    return location;
}

问题是我不知道如何设置params属性以从两个文本框的其他文本框中排除相同的位置。

这是一个有效的JSFiddle

非常感谢任何帮助。

0 个答案:

没有答案