jqueryajax甚至在那里加载结果列表';没什么可质疑的

jqueryajax甚至在那里加载结果列表';没什么可质疑的,jquery,ajax,django,django-templates,django-views,Jquery,Ajax,Django,Django Templates,Django Views,因此,我正在学习jquery和ajax教程,到目前为止,我有一个搜索表单,用于搜索和返回搜索结果。而且效果很好。但是,当我从表单中删除单词或将单词退格时,它会加载列表中的所有状态。我该怎么做才能只在有单词的时候返回,而在表单中没有单词的时候什么也不返回 status.html的代码段: {% block add_account %} <input type="text" id="search" name="search" /> <ul id="search-results"&

因此,我正在学习jquery和ajax教程,到目前为止,我有一个搜索表单,用于搜索和返回搜索结果。而且效果很好。但是,当我从表单中删除单词或将单词退格时,它会加载列表中的所有状态。我该怎么做才能只在有单词的时候返回,而在表单中没有单词的时候什么也不返回

status.html的代码段:

{% block add_account %}
<input type="text" id="search" name="search" />

<ul id="search-results">

</ul>

{% endblock %}
ajax.html:

{% if statuss.count > 0 %}

{% for status in statuss %}
    <li><a href="/status/get/{{status.id}}/">{{status.status}}</a></li>
{% endfor %}

{% else %}

<p>None to show</p>

{% endif %}

@罗宾还了什么?什么意思?请求应该发送,如果有什么东西要搜索我很抱歉,你给的第一个脚本没有返回任何东西,我是说什么都没有。但第二种方法有效。但是,当我在表单中退格时,它仍然显示列表中的最后一个查询。。。我做错了什么?@Robin不用keyup,试着用keypress。对于第一个脚本,必须工作,所以你在某个地方做错了什么。始终检查您的控制台是否有打字错误KeyPress不返回任何内容。我只是复制并粘贴了你给我的脚本。这可能是因为jquery版本吗?
{% if statuss.count > 0 %}

{% for status in statuss %}
    <li><a href="/status/get/{{status.id}}/">{{status.status}}</a></li>
{% endfor %}

{% else %}

<p>None to show</p>

{% endif %}
def search_status(request):
    if request.method == "POST":
        search_text = request.POST['search_text']
    else:
        search_text=''

    statuss = Status.objects.filter(status__icontains = search_text)

    return render(request, 'ajax_search.html', {'statuss':statuss})
$('#search').keyup(function () {
    if (!$.trim(this.value).length) return; //<< returns from here if nothing to search
    $.ajax({
        type: "POST",
        url: "/status/search_status/",
        data: {
            'search_text': $('#search').val(),
                'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
        },
        success: searchSuccess,
        dataType: 'html'
    });
});
(function () {
    var throttleTimer;
    $('#search').keyup(function () {
        clearTimeout(throttleTimer);
        throttleTimer = setTimeout(function () {
            if (!$.trim(this.value).length) return;
            $.ajax({
                type: "POST",
                url: "/status/search_status/",
                data: {
                    'search_text': $('#search').val(),
                        'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()
                },
                success: searchSuccess,
                dataType: 'html'
            });
        }, 100);
    });
}());