Run javascript function when user finishes typing instead of on key up?

前端 未结 29 2092
我寻月下人不归
我寻月下人不归 2020-11-22 04:03

I want to trigger an ajax request when the user has finished typing in a text box. I don\'t want it to run the function on every time the user types a letter because that wo

29条回答
  •  攒了一身酷
    2020-11-22 04:44

    If you need wait until user is finished with typing use simple this:

    $(document).on('change','#PageSize', function () {
        //Do something after new value in #PageSize       
    });
    

    Complete Example with ajax call - this working for my pager - count of item per list:

    $(document).ready(function () {
        $(document).on('change','#PageSize', function (e) {
            e.preventDefault();
            var page = 1;
            var pagesize = $("#PageSize").val();
            var q = $("#q").val();
            $.ajax({
                url: '@Url.Action("IndexAjax", "Materials", new { Area = "TenantManage" })',
                data: { q: q, pagesize: pagesize, page: page },
                type: 'post',
                datatype: "json",
                success: function (data) {
                    $('#tablecontainer').html(data);
                   // toastr.success('Pager has been changed', "Success!");
                },
                error: function (jqXHR, exception) {
                    ShowErrorMessage(jqXHR, exception);
                }
            });  
        });
    });    
    

提交回复
热议问题