简体   繁体   English

Javascript文件/ jQuery未呈现

[英]Javascript file/Jquery not being rendered

I'm pretty sure there's something wrong with the way I'm rendering the file because everything else looks pretty solid, please let me know if it's not. 我很确定我呈现文件的方式有问题,因为其他所有内容看起来都很稳定,请告诉我是否存在问题。 nothing is showing up on my bar where it should since I'm using keyup. 自从我使用keyup以来,我的栏上什么都没有出现。 Not even an error which I think means jquery is not being rendered. 我什至没有一个错误,我认为这意味着没有呈现jquery。 Here;s my code 这是我的代码

javascript file javascript文件

$(function(){

    $('#search').keyup(function() {

        $.ajax({
            type: "POST",
            url: "/search/",
            data: { 
                'search_text' : $('#search').val(),
                'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
            },
            success: searchSuccess,
            dataType: 'html'
        });

    });

});

function searchSuccess(data, textStatus, jqXHR)
{
    $('#search-results').html(data);
}

index.html index.html

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

      <ul id="search-results">

      </ul>

I'm matching id as search but nothing shows up 我将ID匹配为搜索,但没有任何显示

my javascript file is in my static folder->another static->js>then ajax.js so this is how I did <script src="{% static 'js/ajax.js' %}"></script> 我的javascript文件位于我的静态文件夹中->另一个static-> js>然后是ajax.js,所以这就是我的做法<script src="{% static 'js/ajax.js' %}"></script>

Not sure if my python code is right, because it's not being rendered at all. 不知道我的python代码是否正确,因为它根本没有被渲染。 Here;s my python code 这是我的python代码

def search_titles(request):
    if request.method == "POST":
        search_text = request.POST['search_text']
    else:
        search_text = ""

    categories = Category.objects.filter(name__contains=search_text)
    return render_to_response('ajax_search.html',{'categories':categories})

ajax search.html is ajax search.html是

{% if categories.count > 0 %}

{% for category in categories %}
    <li><a href="/category/{{post.category}}">{{ category.title }}</a></li>
{% endfor %}

{% else %}

<li>None to show!</li>

{% endif %}

I think you wanted to use self invoking anonymous function but actually you forgot to call it. 我认为您想使用自调用匿名函数,但实际上您忘了调用它。

$(function(){
    ...
})();

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

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