简体   繁体   中英

How to remove search results with jQuery after remove the characters?

So I am a back-end developer with zero jQuery or JavaScript knowledge, I managed to find a piece of code to create a Ajax search like feature. I bundled it with my Django model and it works. However after I remove characters in the input bar, the results are still displayed.

I would like to see when the input is none, the results also go away. I use this piece of code:

<script type="text/javascript">
    $( document ).ready( function() {
        $( '#q' ).keyup( function() {
            q = $( '#q' ).val();
            $( '#results' ).html( '&nbsp;' ).load( '{% url 'search' %}?q=' + q );
        });
    });
</script>

Try this code. I haven't tested.

<script type="text/javascript">
    $( document ).ready( function() {
        $( '#q' ).keyup( function() {
            q = $( '#q' ).val();
            if(q.length > 0)
               $( '#results' ).html( '&nbsp;' ).load( '{% url 'search' %}?q=' + q );
            else
               $( '#results' ).html( '&nbsp;' )
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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