简体   繁体   中英

How to remove duplicates options from the datalist in HTML using javascript or jquery?

I am working on a search box where I used Flask, MySQL and Ajax to get the search suggestion while writing any query. I am getting response from the ajax call back when I start writing in the search box and I am appending new option which generate lot's of duplicates option because suggestion can be similar in another response so I want to delete that.

Here is the duplicate option problem

(In the Right Side You Can see the duplicates option showing in console) -

在此处输入图像描述

Here is my Ajax -

<script>
    $(document).ready(function(){
        $("#searchbox").on("input", function(e){
            searchtext = $("#searchbox").val();
            
            $.ajax({
                method:"post",
                url:"/searchengine",
            
                data:{text:searchtext},
                success:function(res){
                
                    $.each(res,function(index,value){
                         options =  "<option value="+value.address+">";
                            console.log(options)
                    });
                    $('#results').append(options);
             
                                }
            })

        
                        
            
        });
    })
</script>

Can you please tell me how can I delete these duplicate values from datalist in HTML THANKS IN ADVANCE!

I'd suggest you not just add new values, but form new list of options and replace old one with it. So, instead of $('#results').append(options) you go with $('#results').empty().append(options) . This way you will have only new options and no duplicates (unless there is some in your data).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM