本文介绍了从jQuery Autocomplete中的数组中删除选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组

var metrics = [
        {
            value: "1",
            label: "Sold Listings",
            desc: "sold_listings"
        },
        {
            value: "10",
            label: "New Pendings",
            desc: "new_pendings"
        },
        {
            value: "4",
            label: "All Pendings",
            desc: "all_pendings"
        },
        {
            value: "2",
            label: "New Listings",
            desc: "new_listings"
        },
        {
            value: "3",
            label: "Active Listings",
            desc: "active_listings"
        }
    ];

我想对Selected项进行操作,例如,我将选择Active Listings,该项应从数组中删除.这样,当自动完成功能再次呈现时,就不会显示所选的项目.

What I wanted to do is to the Selected item, for example I will select Active Listings, this item should be deleted from the array. So that when the autocomplete renders again, it won't show the selected item.

//My Idea of removing the item
            $.each(metrics,function(i,val){
                if(val.value == ui.item.value){
                    delete metrics[i];
                }
            });

推荐答案

问题是您没有更新autocomplete的源.
为此,您必须使用以下行.

The problem is that you didn't update the source of autocomplete.
To do it you have to use the following line.

$('.yourAutocomplete').autocomplete('option', 'source', yourArray);

因此,在自动完成选择事件上:

So, on autocomplete select event:

假设#autocomplete是您的input.

select: function(e, ui) {
    metrics = jQuery.grep(metrics, function(element) {
        return element.value != ui.item.value;
    });
    $('#autocomplete').autocomplete('option', 'source', metrics)
    return false;
}

演示

参考:

这篇关于从jQuery Autocomplete中的数组中删除选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:20