使用javascript删除重复的输入值

时间:2016-06-15 06:55:02

标签: javascript jquery ajax

我想根据用户文本输入html进行历史搜索,下面的代码工作正常,但它复制相同的输入。例如:

  1. 用户类型' abcd'在输入文本中单击按钮。
  2. 价值' abcd'存储为历史搜索。
  3. 当用户尝试搜索' abcd'然后再次点击该按钮,历史搜索添加' abcd'同样,它有2&#ab;'值。
  4. 我的js功能:

    function cek() {
       resi_or_code = document.getElementById('code_or_resi').value;
       resi = resi_or_code.split(',');
        if($.trim(resi_or_code) != ''){
         location.href = base_url + 'resi/' + encodeURIComponent(resi_or_code);
    
         }
         if (localStorage.daftar_data){
               daftar_data = JSON.parse(localStorage.getItem('daftar_data'));
               $("#riwayat").toggle();
    
         }
         else {
         daftar_data = [];
         }
    
       for (y in daftar_data){
           var q =  daftar_data[y].resis;
           for (x in resi){
           console.log(q); 
               if (q === resi[x]) 
               {   
               console.log('Value exist');
    
               }else{
               console.log('Value does not exist');
               daftar_data.push({'resis':resi[x]});
               localStorage.setItem('daftar_data', JSON.stringify(daftar_data));
               } 
           }
       }
    
    
    }
    

    我做错了什么?请帮忙。

0 个答案:

没有答案