D
D
devilwish2020-04-23 10:48:44
JavaScript
devilwish, 2020-04-23 10:48:44

How to limit the number of displayed values?

$(document).ready(function(){
   $('#search').keyup(function(){
      $('#result').html('');
      var searchField = $('#search').val();
      var expression = new RegExp(searchField,  "i");
      $.getJSON('goods.json', function(data){
        $.each(data, function(key, value){
          if(value.name.search(expression) != -1 || value.location.search(expression) != -1)
          {
           
            $('#result').append('<li style="list-style-type: none;margin:2px;border-bottom:1px solid #333;"><a href="'+key+'" title=""><img src="'+value.img+'" height:"40" width="40">'+value.name+'</a></li>')
    
        }
        });
      
      });
   });
});

I did a test live search, but it outputs everything from json, how can I limit it to some value?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-04-23
@twobomb

...
      $.getJSON('goods.json', function(data){
        $.each(data.splice(0,10), function(key, value){
          if(value.name.search(expression) != -1 || value.location.search(expression) != -1)
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question