【问题标题】:How to stop keyup ajax call if there are no results?如果没有结果,如何停止 keyup ajax 调用?
【发布时间】:2019-03-28 15:22:50
【问题描述】:

我正在使用 .ajax 调用在 keyup 上进行自动填充搜索。当我输入超过 3 个字符时,它开始发送呼叫。当没有更多结果时,我想停止更多的 ajax 调用,因此用户可以在每次编写字符时继续填充输入而无需新调用。 有人可以帮忙吗?

$(document).ready(function () {
    $("#search-name").keyup(function () {
        var input = $("#search-name").val();
        if (input.length > 3) {
            $.ajax({
                type: "POST",
                url: "inc/autofill_name.php",
                data: 'keyword=' + $(this).val(),
                beforeSend: function () {
                    $("#search-name").css("background", "#FFF");
                },
                success: function (data) {
                    $("#suggesstion-box").show();
                    $("#suggesstion-box").html(data);
                    $("#search-name").css("background", "#FFF");
                }
            });
        }
    });
});

function selectName(val) {
    $("#search-name").val(val);
    $("#suggesstion-box").hide();
}

已添加 PHP 代码:

if(!empty($_POST['keyword'])) {

    $keyword = $_POST["keyword"]; 
    $query ='SELECT * FROM users WHERE name like ' . quote_smart($keyword,1). ' GROUP BY name ORDER BY name LIMIT 30';

    $results = mysqli_query($mysqli, $query);   
    if(!empty($results)) {

        echo '<ul id="user-list">';

        foreach($results as $user) {
            $username = htmlspecialchars(''.$user["name"].'', ENT_QUOTES);

            echo '<li onClick="selectName(\''.$username.'\')">'.$user['name'].'</li>';
        } 
        echo '</ul>';
    } 

}

【问题讨论】:

  • 对你的成功有一个条件,如果有结果填满或者让用户输入
  • 所以没人能解决这个问题?

标签: javascript php jquery ajax


【解决方案1】:

我提出了一个概念关于这样的事情如何可以工作(如果我正确地解释了你的问题)。

编辑的 Javascript 代码

$(document).ready(function () {
//0 => there are results, else equal to the text input length on which no results could be retrieved anymore.
var noMoreResults = 0;

$("#search-name").keyup(function () {
    var input = $("#search-name").val();
    var inputLength = input.length;

    if (inputLength > 3) {
        //Make a Ajax Call if there are results (noreMoreResults == 0) or if the noMoreResults is set AND the inputLength is less than the length on when no results could be retrieved anymore.
        if (noMoreResults == 0 || (noMoreResults !== 0 && inputLength < noMoreResults)) {
            noMoreResults = 0;

            $.ajax({
                type: "POST",
                url: "inc/autofill_name.php",
                data: 'keyword=' + $(this).val(),
                beforeSend: function () {
                    $("#search-name").css("background", "#FFF");
                },
                success: function (data) {
                    //If there aren't any results.
                    if (data.length == 0) {
                        //Set noMoreResults to the input length.
                        noMoreResults = inputLength;

                        return;
                    }

                    $("#suggesstion-box").show();
                    $("#suggesstion-box").html(data);
                    $("#search-name").css("background", "#FFF");
                }
            });
        }
    }
});

});

编辑的 PHP 代码

if(!empty($_POST['keyword'])) {

$keyword = $_POST["keyword"]; 
$query ='SELECT * FROM users WHERE name like ' . quote_smart($keyword,1). ' GROUP BY name ORDER BY name LIMIT 30';

$results = mysqli_query($mysqli, $query);   
if(!empty($results)) {

    echo '<ul id="user-list">';

    foreach($results as $user) {
        $username = htmlspecialchars(''.$user["name"].'', ENT_QUOTES);

        echo '<li onClick="selectName(\''.$username.'\')">'.$user['name'].'</li>';
    } 
    echo '</ul>';
} 
//Empty if there aren't any results (if this isn't the default behaviour already?).
else {
    echo '';
}

}

希望对你有所帮助。

【讨论】:

  • 感谢您。这个想法看起来很有希望,但不幸的是,当我继续编写下一个字符时,它会不断发布新的 ajax 调用。例如。在表中有“约翰”,我开始写约翰..它给了我正确的结果,打赌如果我继续写..athan(Johnathan)(不在数据库中),每次我写信时它都会不断发布新的 ajax 调用.所以,不工作
【解决方案2】:

有一个标志表示没有更多的结果,如果输入了新的关键字前缀则重置它:

$(document).ready(function () {

    let noResult = false;
    let minLastWord = "";

    $("#search-name").keyup(function () {
        var input = $("#search-name").val();

        if (!input.startsWith(minLastWord)) {
            noResult = false;
        }

        if ((input.length > 3) && !noResult) {
            $.ajax({
                type: "POST",
                url: "inc/autofill_name.php",
                data: 'keyword=' + $(this).val(),
                beforeSend: function () {
                    $("#search-name").css("background", "#FFF");
                },
                success: function (data) {
                    $("#suggesstion-box").show();
                    $("#suggesstion-box").html(data);
                    $("#search-name").css("background", "#FFF");

                    if (data == "") {//You can create your own condition of "emptiness"
                       noResult = true;
                       minLastWord = input;
                    } else {
                       noResult = false;
                    }

                }
            });
        }
    });
});

【讨论】:

  • 嗨,刚刚试过你的代码。对不起,这也不起作用。当我继续在输入中写入时,我的 Chrome 控制台显示 ajax 在每个字符之后不断发布到 autofill_author.php。我可以写越来越多的字符,并且在每个 jquery 发送新的 ajax 调用之后
  • php没有结果时的输出是什么?
  • 嗯,它是'';但我也尝试过你写的,我自己的情况,echo = 1;然后编辑您的代码 if (data == "1") 。它只是在每个写入字符后不断发送新的 ajax 调用
猜你喜欢
  • 2021-04-20
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多