我已经使用jQuery. 这是发送搜索词的代码,之后我收到Json用于制作匹配搜索项目列表的代码。
问题是每次keyup我删除所有匹配的项目:
    $("#realPlaceForSearchItems").html(""); 
因为如果我不输入“ pro ”然后输入“ d ”,我会在搜索产品时得到重复。(我正在将列表项附加到列表中)是否有可能以某种方式删除与“ prod ”不匹配的元素(当然之前与“ prod ”匹配)并且匹配 prod 的元素在键入“ d ”后保持不变.
 $("#searchInput").keyup(function () {
    $this = $(this);
    $('#realPlaceForSearchItems').show();
    $("#realPlaceForSearchItems").html("");
    var seachedTerm=$this.val();
    if ($this.val().length> 2)
    {
        $("#realPlaceForSearchItems").html("");
        $('#realPlaceForSearchItems').show();
        $.ajax({
            type: "POST",
            url: ROOT + "Filter/Search/",
            data: {term: $this.val()},
            success: function (data)
            {
                var Link = $("#searchTemplate>li>a");
                var placeForProductId=$("#searchTemplate>li>a>input");
                var placeForPicture = $("#searchTemplate>li>a>div>img");
                var placeForProductName = $("#searchTemplate>li>a>div>div");
                var placeForPrice= $("#searchTemplate>li>a>div>span");
                $.each(data.productsWereSeached, function () {
                    console.log("sddsd", data.totalrows);
                    var imagesFolder="/Content/images/";
                    var pathToProduct="/ProductDetails/Index/"
                    var slash = "/";
                    Link.attr("href", pathToProduct + this.Id);
                    placeForProductId.val(this.Id);
                    if (this && this.Picture) //for the case there is no any picture there would be error cant read propery or undefined
                        placeForPicture.attr("src", imagesFolder + this.Id + slash + this.Picture.FileName);
                    else
                        placeForPicture.attr("src", "");
                    placeForProductName.html(this.Name);
                    placeForPrice.html((parseFloat(this.Price) / 100.0).toString() + " kn");
                    $listItem = $("#searchTemplate").html();
                    $("#realPlaceForSearchItems").append($listItem);
                });
                $("#nOfMatchedProducts").val(data.totalrows);
                if (data.totalrows > 2)
                {
                    var searchurl="/Search/ShowMoreSearched?term="
                    $showMoreItem = $("#showMoreItem").html();
                    $("#showMoreItem>li>a").attr("href",searchurl+seachedTerm);
                    $("#realPlaceForSearchItems").append($showMoreItem);
                }
            },
            failure: function ()
            {
            }
        });
    }
});