搜索/过滤json功能需要编辑

时间:2015-12-11 13:33:40

标签: javascript jquery html json

我有一个get json函数,可以在面板等中显示我的图像:

$.getJSON('iproducts.json',function(products){

var output = "";
$.each(products.appleitems, function(i, product) { 

    output += 
        "<div class=\"col-xs-12 col-sm-6 col-md-3\"><div class='panel panel-default'><div class='panel-footer'><h4 class='text-center'>"  
        + products.appleitems[i].Product_id 
        + "</h4></div>" + "<img src ='" + products.appleitems[i].Imgpath + "'  style='width:100%;height:250px; display: block;' id='appleinfo_" 
        + products.appleitems[i].Product_id + 
        "' /><p class='lead text-justify'>" + products.appleitems[i].Information
        + "</p><div class='panel-footer'><button class='btn btn-primary btn-block'>&pound;" + products.appleitems[i].Price+"</button></div></div></div>";
    });

$("#container").html(output);
});

它还包含一个搜索/过滤器文本框和按钮,使项目显示在我的代码中。我修改了代码,现在搜索功能不再有效我已经尝试了所有的东西。这是实际的搜索框:

<div class="container search-container">
<form class="form-search form-inline"> 
    <input type="text" class="input-medium search-query" placeholder="Search"> <button type="submit" class="btn">Search</button> 
</form>

以及我用来过滤这些脚本的脚本:

$('.form-search').on('submit',function(){return false;}); 
$('.form-search .btn').on('click', function(e){ 
var query = $.trim($(this).prevAll('.search-query').val()).toLowerCase(); 
regex = new RegExp(query, "i");
$.getJSON('iproducts.json', function (products) {
    var output;
    $.each(products.appleitems, function (i, product) {

        if (product.Product_id.search(regex) != -1) {
            output+= "<div class=\"col-xs-12 col-sm-6 col-md-3\"><div class='panel panel-default'><div class='panel-footer'><h4 class='text-center'>"  
        + products.appleitems[i].Product_id 
        + "</h4></div>" + "<img src ='" + products.appleitems[i].Imgpath + "'  style='width:100%;height:250px; display: block;' id='appleinfo_" 
        + products.appleitems[i].Product_id + 
        "' /><p class='lead text-justify'>" + products.appleitems[i].Information
        + "</p><div class='panel-footer'><button class='btn btn-primary btn-block'>&pound;" + products.appleitems[i].Price+"</button></div></div></div>";
        }
            $('#placeholder').html(output);

            console.log(products.appleitems[i]);
        }
    )
});
});

1 个答案:

答案 0 :(得分:0)

尝试将搜索脚本上的#placeholder更改为#container:

$( '#占位符')HTML(输出);

更改为:

$( '#容器')HTML(输出);

希望这有帮助