Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在jquery中创建ajax过滤器?_C#_Jquery_Asp.net Mvc - Fatal编程技术网

C# 如何在jquery中创建ajax过滤器?

C# 如何在jquery中创建ajax过滤器?,c#,jquery,asp.net-mvc,C#,Jquery,Asp.net Mvc,我试图做一个过滤,用户将按姓氏搜索。现在我在键盘上有它,但当然这会产生太多的ajax请求,所以我宁愿在几个键或类似的东西之后有它 我将以表格的形式返回所有结果(我有一个局部视图,该视图生成一个包含信息的表格) 你知道我该怎么做吗。我猜逻辑类似于自动完成,我知道它们可以设置查询db之前的击键次数 像 $(function () { $('#LastName').keypress(function () { if($(this).val().length > 4

我试图做一个过滤,用户将按姓氏搜索。现在我在键盘上有它,但当然这会产生太多的ajax请求,所以我宁愿在几个键或类似的东西之后有它

我将以表格的形式返回所有结果(我有一个局部视图,该视图生成一个包含信息的表格)


你知道我该怎么做吗。我猜逻辑类似于自动完成,我知道它们可以设置查询db之前的击键次数

$(function ()
{
    $('#LastName').keypress(function ()
    {
        if($(this).val().length > 4)
        {
            $.post('ActionMethod', { 'lastName': $(this).val() }, function (response)
            {
                 $('#results').html(response);
            });
        }
    });
});

我认为您应该使用超时并延迟ajax调用的执行。如果执行前出现按键,则重置计时器

$(function ()
    {
        var timer;
        $('#LastName').keypress(function ()
        {
            var _this = this; // we need to store the this to a variable since it will be called out of context from the timeout
            clearTimeout(timer);
            timer = setTimeout(function(){
                         $.post('ActionMethod', { 'lastName': $(_this).val() }, function (response)
                         {
                             $('#results').html(response);
                         });
                       }, 500); // delay for 500 milliseconds
        });
    });

您可以预先加载名称的所有唯一值,并使用该数组。如果你没有太多的值,这就行了。对于你的情况,我想,我怀疑会有成千上万个独一无二的姓氏


您还可以设置ajax请求之间的时间限制,而不是击键次数。(就像加比建议的那样)

提供了一种处理速率限制的简单方法。

我正在考虑,但是4太长了。我的意思是很多亚洲名字都有两个字符长(李、马等)。所以这不会真正起作用。我在想,也许当他们停止打字的时候,它就可以了。
$(function ()
    {
        var timer;
        $('#LastName').keypress(function ()
        {
            var _this = this; // we need to store the this to a variable since it will be called out of context from the timeout
            clearTimeout(timer);
            timer = setTimeout(function(){
                         $.post('ActionMethod', { 'lastName': $(_this).val() }, function (response)
                         {
                             $('#results').html(response);
                         });
                       }, 500); // delay for 500 milliseconds
        });
    });