简体   繁体   English

如何在jQuery自动完成中添加清除按钮(MVC剃刀)

[英]How to add a clear button in jquery autocomplete(mvc razor)

I have a search functionality which already works by searching the data that the user requests. 我有一个搜索功能,已经可以通过搜索用户请求的数据来工作。 I would like to add a clear button for the user to be able to clear the search bar, at the moment the user has to clear the search using the "backspace" button and press "enter to go back the page with all the data. I am a expert in front end so would appreciate some help thank you in advance. 我想为用户添加一个清除按钮,使其能够清除搜索栏,此刻,用户必须使用“退格”按钮清除搜索,然后按“输入以返回所有数据的页面”。我是前端方面的专家,因此需要一些帮助,在此先感谢您。

Javascript 使用Javascript

   $(function () {
        $("#SearchString").autocomplete({
            source: '@Url.Action("GetUserJSON")',
            minLength: 1

        })
    });

    $(function () {
        $("#SearchString").focus();
    });



    $(function () ) {
        $("#clearbutton").click(function () {
            $('#SearchString').autocomplete('close');
        });
    };

Razor HTML 剃刀HTML

   @using (Html.BeginForm("Index", "User", FormMethod.Get, null))
    {
        <div class="search-wrap">
            @Html.TextBoxFor(m => m.SearchString, new { id = "SearchString", @class = "lookup txt-search js-autocomplete-submit", @placeholder = "Search", @type ="search" })
            @*<img  src="~/Content/Images/close.png" id ="closebutton"/>*@ 
            <button type="button"  id="clearbutton">Click Me!</button>
            <i onclick="submitform()" class="btn-search fa fa-search"></i>
        </div>        
    }

C# Class where data get pull from 从数据中提取数据的C#类

  public JsonResult GetUserJSON(string term)
    {
        var stores = (from st in UserLogic.GetUserIndex(1, term).IndexList
                      select new { st.Username, st.FirstName, st.LastName }).ToList();

        List<String> returnList = new List<string>();

        foreach (var item in stores)
        {
            if (item.Username.ToString().ToUpper().StartsWith(term.ToUpper()))
            {
                returnList.Add(item.Username.ToString());
            }
            else if (item.FirstName.ToUpper().Contains(term.ToUpper()))
            {
                returnList.Add(item.FirstName);
            }
            else if (item.Username.ToUpper().Contains(term.ToUpper()))
            {
                returnList.Add(item.Username);
            }             
        }

        returnList = returnList.Distinct().OrderByAlphaNumeric(s => s).ToList();
        return Json(returnList, JsonRequestBehavior.AllowGet);
    }

I think this is what you need: 我认为这是您需要的:

    $(function () {
            $("#clearbutton").click(function () {
                $('#SearchString').autocomplete('close');
                $("#SearchString").val("")
            });
    });

Add $("#SearchString").val("") to your clearbutton click event $("#SearchString").val("")到您的clearbutton click事件中

Edit: You have mistyped the function for clearSearch 编辑:您为clearSearch输入了错误的功能

this is working example 这是工作的例子

please try using this 请尝试使用这个

$("#clearbutton").click(function () {
        $('#SearchString').autocomplete('close').val('');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM