在实时搜索中搜索多个值

时间:2017-02-21 00:00:10

标签: javascript json

我的网站上有一个实时搜索表单,它连接到我的json文件,让我搜索json数据中的所有不同成分,并显示项目及其所有成分。但是我的脚本只允许我一次搜索一种成分。有没有人知道我如何搜索,例如,成分一,然后在搜索栏中输入成分2以显示这些项目?

$(window).load(function(){
        $('#search').keyup(function(){
            var searchField = $('#search').val();
            var regex = new RegExp(searchField, "i");
            var output = '<div class="row">';
            var count = 1;
            $.getJSON('new_cocktail.json', function(data) {
              $.each(data, function(key, val){
                if ((val.cocktails.search(regex) != -1) || (val.ingredient_one.search(regex) != -1)|| (val.ingredient_two.search(regex) != -1)|| (val.ingredient_three.search(regex) != -1)|| (val.ingredient_four.search(regex) != -1)|| (val.ingredient_five.search(regex) != -1)|| (val.ingredient_six.search(regex) != -1)|| (val.ingredient_seven.search(regex) != -1)|| (val.ingredient_eight.search(regex) != -1|| (val.ingredient_nine.search(regex) != -1))) {
                  output += '<div class="col-md-6 well">';
                  output += '<div class="col-md-7">';
                  output += '<h4>' + val.cocktails + '</h4>';
                  output += '<h5>' + val.ingredient_one + '</h5>';
                  output += '<h5>' + val.ingredient_two + '</h5>';
                  output += '<h5>' + val.ingredient_three + '</h5>';
                  output += '<h5>' + val.ingredient_four + '</h5>';
                  output += '<h5>' + val.ingredient_five + '</h5>';
                  output += '<h5>' + val.ingredient_six + '</h5>';
                  output += '<h5>' + val.ingredient_seven + '</h5>';
                  output += '<h5>' + val.ingredient_eight + '</h5>';
                  output += '<h5>' + val.ingredient_nine + '</h5>';
				  output += '</div>';
                  output += '</div>';                    
                  if(count%2 == 0){
                    output += '</div><div class="row">'
                  }
                  count++;
                }
              });
              output += '</div>';
              $('#results').html(output);
            }); 
        });
      });
	
	</script>

0 个答案:

没有答案